- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
Latest commit
27 lines (26 loc) · 1.28 KB
/
Copy pathpython.py
File metadata and controls
27 lines (26 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
importrequests#J个是请求模块
importparsel#数据筛选(xml, re, bs4) 👉"bs4"这东西🐕都不用好久没更新了🤭 7年左右没更了!
importos
url="https://www.jdlingyu.com/tuji"#网页链接
response=requests.get(url) #1.发送网络请求
# print(response.text) #打印获取到的内容
html_data=response.text#2.获取数据网页代码
selector=parsel.Selector(html_data) #3.筛选数据
url_list=selector.xpath('//div[@class="post-info"]/h2/a/@href').getall()
# print(url_list)
fordetail_urlinurl_list: #4.发送网络请求到相册页面
# print(detail_url)
detail_html=requests.get(detail_url).text
# print(detail_html)
detail_selector=parsel.Selector(detail_html) #5.筛选数据 图片地址
title=detail_selector.xpath('//h1/text()').get()
print('正在爬取:{title}') #爬取标题
ifnotos.path.exists('img/'+title):
os.mkdir('img/'+title)
img_list=detail_selector.xpath('//div[@class="entry-content"]/p/img/@src').getall() #批量爬取页面里的图片
forimginimg_list:
img_data=requests.get(img).content
img_title=img.split('/')[-1]
withopen(f'img/{title}/{img_title}', mode='wb') asf:
f.write(img_data)
print('正在爬取:{img_title}')