class HtmlDownloader(object):
def download(self,url):
if url is None:
return None
reponse=urllib2.urlopen(url)
if response.getcode() != 200:
return None
return response.read()
def download(self,url):
if url is None:
return None
reponse=urllib2.urlopen(url)
if response.getcode() != 200:
return None
return response.read()
2017-10-30
getcode() 200頁面請求的狀態(tài)值,
分別有:
200請求成功、
303重定向、
400請求錯誤、
401未授權(quán)、
403禁止訪問、
404文件未找到、
500服務(wù)器錯誤
分別有:
200請求成功、
303重定向、
400請求錯誤、
401未授權(quán)、
403禁止訪問、
404文件未找到、
500服務(wù)器錯誤
2017-10-30
count = 1
print 'craw %d : %s ' % (count,new_url)
if count ==1000:
break
count = count +1
print 'craw %d : %s ' % (count,new_url)
if count ==1000:
break
count = count +1
2017-10-30
import urllib2
import cookielib
print '第三種方法'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(url)
print response3.getcode()
print cj
print response3.read()
import cookielib
print '第三種方法'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(url)
print response3.getcode()
print cj
print response3.read()
2017-10-30
入口URL:root_url
應(yīng)用spider.craw(root_url)來啟動爬蟲
if _name_=='_main_':
root_url='...'
obj_spider=spidermain()
obj_spider.craw(root_url)
應(yīng)用spider.craw(root_url)來啟動爬蟲
if _name_=='_main_':
root_url='...'
obj_spider=spidermain()
obj_spider.craw(root_url)
2017-10-29
add_new_url : 向管理器中添加一個新的url
add_new_urls:向管理器中添加批量的url
異常處理:
except:
print 'craw failed'
add_new_urls:向管理器中添加批量的url
異常處理:
except:
print 'craw failed'
2017-10-29
spider_main (爬蟲總調(diào)度程序) url_manager(url管理器) html_downloader(下載器)
html_parser(html解析器) html_outputer(將數(shù)據(jù)處理好的數(shù)據(jù)寫出到 html 的頁面)
html_parser(html解析器) html_outputer(將數(shù)據(jù)處理好的數(shù)據(jù)寫出到 html 的頁面)
2017-10-29
抓取內(nèi)容:
url格式
數(shù)據(jù)格式
網(wǎng)頁編碼
一、URL格式:
詞條頁面URL:/view/125370.htm
這不是一個完整的URL 在代碼中我們需要加上baidubke使其成為完整的URL才能爬取
二,數(shù)據(jù)格式:
標(biāo)題: dd class h1
簡介:div class lemma- summary
三,頁面編碼:utf-8
url格式
數(shù)據(jù)格式
網(wǎng)頁編碼
一、URL格式:
詞條頁面URL:/view/125370.htm
這不是一個完整的URL 在代碼中我們需要加上baidubke使其成為完整的URL才能爬取
二,數(shù)據(jù)格式:
標(biāo)題: dd class h1
簡介:div class lemma- summary
三,頁面編碼:utf-8
2017-10-29