您好,登錄后才能下訂單哦!
#encoding:utf-8
import urllib
import urllib.request
from lxml import etree
?
class Spider(object):
def __init__(self):
self.tiebaName="校花"
self.beginPage=1
self.endPage=3
self.url="http://tieba.baidu.com/f?"
self.ua_header = {"User-Agent" : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1 Trident/5.0;"}
self.fileName=1
????
????#構造url
def tiebaSpider(self):
for page in range(self.beginPage,self.endPage+1):
pn=(page-1)*50
wo={'pn':pn,'kw':self.tiebaName}
word=urllib.parse.urlencode(wo)
myurl=self.url+word
self.loadPage(myurl)
?
????#爬取頁面內容
def loadPage(self,url):
req=urllib.request.Request(url,headers=self.ua_header)
data=urllib.request.urlopen(req).read()
?
html=etree.HTML(data)
links=html.xpath('//div[@class="threadlist_lz clearfix"]/div/a/@href')
?
for link in links:
link="http://tieba.baidu.com"+link
self.loadImages(link)
?
?
#爬取帖子詳情頁,獲得圖片的鏈接
def loadImages(self,link):
req=urllib.request.Request(link,headers=self.ua_header)
data=urllib.request.urlopen(req).read()
html=etree.HTML(data)
links=html.xpath('//img[@class="BDE_Image"]/@src')
for imageslink in links:
self.writeImages(imageslink)
?
?
#通過圖片所在鏈接,爬取圖片并保存圖片到本地
def writeImages(self,imagesLink):
function(){ //技術指標 http://www.fx61.com/faq/muniu/437.html
print("正在存儲圖片:",self.fileName,"....")
?
image=urllib.request.urlopen(imagesLink).read()
?
#保存圖片到本地
file=open(r"C:\file\img\\"+str(self.fileName)+".jpg","wb")
file.write(image)
?
file.close()
?
self.fileName+=1
?
if __name__ == '__main__':
mySpider=Spider()
?
mySpider.tiebaSpider()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。