python爬取jsp网页_帮MM用python抓取index及一级目录下的所有符合要求的网页
MM要學(xué)js,但是上不了網(wǎng),要求我?guī)退丫W(wǎng)上一個教程的相關(guān)內(nèi)容抓出來,于是就有了下面的代碼:
import urllib2
import urllib
import re
from sgmllib import SGMLParser
class URLLister(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self.urls = []
def start_a(self, attrs):
href = [v for k, v in attrs if k=='href']
if href:
self.urls.extend(href)
js_root_url = "http://www.w3school.com.cn/js/"
#ep_root_url = "http://www.w3school.com.cn"
index_url = "index.asp"
f = urllib2.urlopen(js_root_url + index_url)
webfile = urllib.urlopen(js_root_url + index_url).read()
fp = file('index.asp', 'w+')
fp.write(webfile)
fp.close()
if f.code == 200:
parser = URLLister()
parser.feed(f.read())
f.close()
#url_pattern = re.compile(r'(^/js/js_|^/tiy/)\D*')
url_js_pattern = re.compile(r'^/js/js\D*')
#url_example_pattern = re.compile(r'^/tiy/\D*')
url_sub_js_pattern = re.compile(r'^/js/js')
for url in parser.urls:
if url_js_pattern.search(url):
url = url_sub_js_pattern.sub('js', url)
webfile = urllib.urlopen(js_root_url + url).read()
fp = file( url , 'w+')
fp.write(webfile)
fp.close()
但是現(xiàn)在還是有問題存在的,最明顯的是,單擊index頁上的超鏈接無法訪問抓取到的一級頁面
總結(jié)
以上是生活随笔為你收集整理的python爬取jsp网页_帮MM用python抓取index及一级目录下的所有符合要求的网页的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 请问鸿扬家装与东易日盛哪个好些?
- 下一篇: mysql 连接查询_Swoole 实战