最近研究了一下PYTHON,都说很好用很强大 ,用一个爬虫程序试验 了一把,效果还不错,原教程地址http://t.cn/AiRQ3fOP
不过发现原著中所说的网页结构有所调整,所以也在原著基础上做了相应的调整,结果还成功了。不过好像没有什么 反反爬技术,继续多多学习,已经爬了近万张图片,有想要图片的可以留下邮箱。一会再换个网站试试。
本文图片来自http://t.cn/8kAn4Sj
修改后代码:
import requests
from bs4 import BeautifulSoup
from time import sleep
header ={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'cookie':'UM_distinctid=1663384985d6fd-03db9719bc459f-8383268-1fa400-1663384985f5c5; CNZZDATA1256110375=1639678596-1538460026-%7C1538460026; pgv_pvi=6300302336; pgv_si=s7801597952; yunsuo_session_verify=61e64cd8bf75fa31a03c96c3195f46f5'}
topic_count = 0
img_count = 0
err_count = 0
for i in range(1,119):
try:
#根据规律遍历生成98个页面地址
url = 'http://t.cn/AiRQ3f03
#使用requests的get方法访问这98个页面
r = requests.get(url=url,headers=header,timeout=30)
#将返回的页面内容通过beautifulsoup的网页解析器解析出来
r_html = BeautifulSoup(r.text,'html.parser')
#查找class名字为'con'的div
html_board = r_html.find('div',class_='cos_list_con')
#遍历class名字为‘pic imgholder’的a标签
for topic_link in html_board.find_all('a',class_='elli'):
sleep(5)
topic_count+=1
print('访问第'+str(topic_count)+'个帖子')
# 访问每一个帖子
tmurl="http://t.cn/R7MHG3t"+topic_link.get('href')
r_topic = requests.get(url=tmurl, headers=header, timeout=30)
# 将帖子内容解析为html网页
topic_html = BeautifulSoup(r_topic.text, 'html.parser')
# 找出帖子的标题
topic_title = topic_html.find('div', class_='title').find('h1').text
#print(topic_title)
# 找出这个帖子中的贴图区域
topic_board = topic_html.find('div', class_='con')
#print(topic_board)
# 从贴图区域找出所有的img标签
title_count=0
for img_link in topic_board.find_all('img'):
# 从img标签中获取链接
#print(img_link.get('src'))
title_count += 1
img_count += 1
#print(topic_title+str(title_count))
#下载图片保存到d盘的img文件夹
tmimgurl="http://t.cn/R7MHG3t"+img_link.get('src')
#print(tmimgurl)
img_file = requests.get(url=tmimgurl,headers=header,timeout=30)
print(img_file),"333"
img_path = 'd:/img/'+topic_title+str(title_count)+'.jpg'
print(img_path),"444"
with open(img_path,'wb') as f:
f.write(img_file.content)
print('图片已保存=====>'+img_path)
except:
err_count+=1
print('出错,跳过'+str(err_count))
print('共爬取到'+str(topic_count)+'个帖子链接')
print('共爬取到'+str(img_count)+'张图片地址')
print('出错的帖子:'+str(err_count)+'个')
网站现在可能改版了,所以以上代码只做参考 ,可能要根据现在的结构再调整代码