資源簡介
煎蛋網(wǎng)圖片爬蟲
代碼片段和文件信息
import?urllib.request
import?os
import?base64
def?url_open(url):
????headers?=?{‘User-Agent‘:‘Mozilla/5.0?(Windows?NT?6.1;?WOW64;?rv:23.0)?Gecko/20100101?Firefox/23.0‘}??
????req?=?urllib.request.Request(urlheaders=headers)
????response?=?urllib.request.urlopen(req)
????html?=?response.read()
????return?html
????
def?get_page(url):
????html?=?url_open(url).decode(‘utf-8‘)
????a?=?html.find(‘current-comment-page‘)+23#‘‘‘偏移23個字符到頁數(shù)位置‘‘‘
????b?=?html.find(‘]‘a(chǎn))
????return?html[a:b]
def?find_imgs(url):
????html?=?url_open(url).decode(‘utf-8‘)
????img_addrs?=[]
????a?=?html.find(‘img?src=‘)
????
????while?a?!=?-1:
????????b?=?html.find(‘.jpg‘a(chǎn)a+255)#從a開始尋找jpg,截止到a+255,既最大字符數(shù)
????????if?b?!=?-1:
????????????img_addrs.append(‘http:‘+?html[a+9:b+4])#+9是‘img?src=‘到圖片鏈接前的字符數(shù),+4是添加到.jpg的末尾
????????else:
?????????
- 上一篇:python對于文件和異常的處理
- 下一篇:豆瓣電影排名250爬取,并存excel
評論
共有 條評論