一个简单的Python 爬虫源码,网站似乎是 WrodPress ,爬虫采集的是网站里的作品信息,包括文字内容及图片,其中图片的下载采集采用了简单的多线程爬取下载。
通过抓包可以简单的获取分页数据,以及相应的获取想要的数据内容,网站结构比较简单明了,适合爬虫新人练手学习使用,附上完整源码供参考和学习使用。
如感兴趣可以自行尝试爬取下载,注意爬取限制一下时间及频率。
附完整源码参考
# -*- coding: UTF-8 -*-
# Fuseproject @公众号:eryeji
# https://fuseproject.com/work/hive-view/#product
import requests
from lxml import etree
import time
import random
import re
import threading
import os
def get_ua():
ua_list = [
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36Chrome 17.0',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0Firefox 4.0.1',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11',
]
ua=random.choice(ua_list)
return ua
def get_hrefs(i):
url=f'https://fuseproject.com/disciplines/product/page/{i}/'
headers={
"User-Agent":get_ua()
}
response=requests.get(url=url,headers=headers,timeout=6)
print(response.status_code)
html=response.content.decode('utf-8')
#print(html)
tree=etree.HTML(html)
hrefs=tree.xpath('//article[@class="tease tease--casestudy"]/a/@href')
print(len(hrefs))
print(hrefs)
for href in hrefs:
get_detail(href)
time.sleep(3)
def get_detail(href):
headers = {
"User-Agent": get_ua()
}
response = requests.get(url=href, headers=headers, timeout=6)
print(response.status_code)
html = response.content.decode('utf-8')
#print(html)
tree = etree.HTML(html)
h1=tree.xpath('//h1/text()')[0]
pattern = r"[\/\\\:\*\?\"\<\>\|]"
h1=re.sub(pattern, "_", h1) # 替换为下划线
print(h1)
path = f'{h1}/'
os.makedirs(path, exist_ok=True)
print(f">> 生成保存目录 {h1} 文件夹成功!")
ptext=tree.xpath('//div[@class="p--l"]/p/text()')[0]
print(ptext)
with open(f'{path}{h1}.txt','w',encoding='utf-8') as f:
f.write(f'{h1}\n{ptext}')
print(f">> 保存 {h1}.txt 文件成功!")
imgs=tree.xpath('//picture[@class="lazy r r--resp"]/img/@data-src')
print(len(imgs))
print(imgs)
down_imgs(path, imgs)
# 3次重试
def get_resp(url):
i = 0
while i < 4:
try:
headers = {
"User-Agent":get_ua()
}
response = requests.get(url, headers=headers, timeout=10)
print(response.status_code)
return response
except requests.exceptions.RequestException:
i += 1
print(f">> 获取网页出错,6S后将重试获取第:{i} 次")
time.sleep(i * 2)
def down_imgs(path,imgs):
threadings = []
for img in imgs:
t = threading.Thread(target=get_img, args=(path,img))
threadings.append(t)
t.start()
for x in threadings:
x.join()
print(f"恭喜,多线程下载图片完成!")
#下载图片
def get_img(path,img_url):
img_name = img_url.split('/')[-1]
img_url=f'https://fuseproject.com{img_url}'
r = get_resp(img_url)
time.sleep(1)
with open(f'{path}{img_name}', 'wb')as f:
f.write(r.content)
print(f">> {img_name}下载图片成功")
def main():
for i in range(1,12):
print(f">> 正在爬取第{i}页数据..")
get_hrefs(i)
time.sleep(i)
if __name__=='__main__':
main()
推荐阅读
手把手教你多线程下载获取图片
热门壁纸多线程采集下载源码
美女图异步爬虫案例小姐姐我全都要
微博用户主页小姐姐图片采集爬虫
图片爬虫多进程及多线程的使用例子
·················END·················
你好,我是二大爷,
革命老区外出进城务工人员,
互联网非早期非专业站长,
喜好python,写作,阅读,英语
不入流程序,自媒体,seo . . .
公众号不挣钱,交个网友。
读者交流群已建立,找到我备注 “交流”,即可获得加入我们~
听说点 “在看” 的都变得更好看呐~
关注关注二大爷呗~给你分享python,写作,阅读的内容噢~
扫一扫下方二维码即可关注我噢~
关注我的都变秃了
说错了,都变强了!
不信你试试
扫码关注最新动态
公众号ID:eryeji