Python爬虫之贴吧图片
今天我们来爬取java吧的图片,开始写代码之前先要找到页面的url,以及对应图片的url
from urllib import request
import urllib
import re
import time
from lxml import html
etree=html.etree
#https://tieba.baidu.com/f?kw=java&ie=utf-8&pn=0 第一页
#https://tieba.baidu.com/f?kw=java&ie=utf-8&pn=50 第二页
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362'}
url=r'http://tieba.baidu.com'
class Spider():
def __init__(self):
self.tiebaName='java'
self.beginPage=1
self.endPage=1
self.url=r'http://tieba.baidu.com/f?'
self.fileName=1
#获取页面的网址
def tiebaSpider(self):
for page in range(self.beginPage-1,self.endPage):
pn=page*50
kw={'kw':'java'}
urll=urllib.parse.urlencode(kw)
fullurl=self.url+urll+'&ie=utf-8&pn='+str(pn)
self.tiebaText(fullurl)
#获取页面信息
def tiebaText(self,fullurl):
req=request.Request(fullurl,headers=header)
response=urllib.request.urlopen(req).read().decode()
pat=r'href="(.*?)"(.+)target="_blank"(.+)class="j_th_tit "'
res=re.compile(pat,re.I)
data=res.findall(response)
for i in range(0,len(data)):
data1=data[i][0]
link=url+data1
req1=request.Request(link,headers=header)
response1=urllib.request.urlopen(req1).read().decode()
self.tiebaImage(response1)
#获取图片网址,并写入本地
def tiebaImage(self,response1):
res=etree.HTML(response1)
Imagelink=res.xpath('//img[@class="BDE_Image"]/@src')
for imagelink in Imagelink:
print(imagelink)
image=urllib.request.urlopen(imagelink).read()
print('正在存储图片:',self.fileName,'.........')
f=open(r'C:\Users\lenovo\Pictures\Saved Pictures\\'+str(self.fileName)+'.jpg','wb')
f.write(image)
time.sleep(0.5)
f.close()
self.fileName+=1
if __name__=='__main__':
mySpider=Spider()
mySpider.tiebaSpider()
运行程序:
找到存储图片的位置,查看图片:
这样java贴吧的图片就获取成功了!!!