|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import requests
from lxml import etree
l
tieba_name = input('请输入你要爬取的贴吧:')
startPage = int(input('开始页:'))
endPage = int(input('结束页:'))
url = 'https://tieba.baidu.com/f?'
for page in range(startPage,endPage+1):
yema = (page - 1) * 50
response = requests.get(url,params={'kw':tieba_name,'pn':yema})
html = response.text # 网页源代码
# print(html)
# 第二步 找到每条链接
element = etree.HTML(html)
link_list = element.xpath('//a[@class="j_th_tit "]/@href')
for link in link_list:
fulllink = 'https://tieba.baidu.com/'+link
# 请求链接地址
tiezi_resp = requests.get(fulllink)
tiezi_html = tiezi_resp.text
img_link = etree.HTML(tiezi_html)
img_list = img_link.xpath('//img[@class="BDE_Image"]/@src')
for my_img in img_list:
# 请求图片的链接地址
img_response = requests.get(my_img)
img = img_response.content
# 获取图片的名字
img_name = my_img[-10:]
# print(img_name)
f = open('img/'+img_name,'wb')
f.write(img)
f.close()
|
|