import requests
import os
from lxml import etree
if not os.path.exists('C:\\Users\\HP\\Desktop\\bizhi\\'):
os.makedirs('C:\\Users\\HP\\Desktop\\bizhi\\')
url='http://xinzhuobu.com/?cat=1&order=hot'
headers={"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36 Edg/97.0.1072.76"}
page=requests.get(url=url,headers=headers).text
tree=etree.HTML(page)
img_list=tree.xpath('//div[@class="site"]/div[@class="site-content"]/div[@class="container"]/div[@class="row"]//div[@class="content-area"]//div[@class="row posts-wrapper"]/div[@class="col-lg-1-5 col-6 col-sm-6 col-md-4 col-lg-3"]')
a='1'
for i in img_list:
img_src=i.xpath('./article/div[@class="entry-media"]/div[@class="placeholder"]//img/@data-src')[0]
img_name=a+'.jpg'
a=str(int(a)+1)
imgg=requests.get(url=img_src,headers=headers).content
img_path="C:\\Users\\HP\\Desktop\\bizhi\\"+img_name
with open(img_path,'wb')as f:
f.write(imgg)
print(img_name,'下载成功')
爬虫练习03
最新推荐文章于 2024-05-29 14:13:29 发布