马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import os
baseurl = "http://sc.chinaz.com/tupian/renwutupian"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}
# 获取整页数据
for i in range(1,30):
if i ==1:
url=baseurl+'.html'
else:
url=baseurl+'_'+str(i)+'.html'
response = requests.get(url=url,headers=headers)
page_text = response.text
print(url)
# 获取网页中的图片
path = os.getcwd()
tree = etree.HTML(page_text)
img_list = tree.xpath('//*[@id="container"]/div')
for i in img_list:
img = i.xpath('./div/a/img/@src2') # 图片懒加载,src2为实际地址,这个在有些地方需要注意
img_url = "".join(img)
img_data = requests.get(url=img_url,headers=headers).content # 生成图片
# 生成图片名称
img_name = img_url.split('/')[-1] # 以图片链接最后的部分作为名字
print(img_name)
# 图片存储的路径
img_path = path+'\\mm\\'+img_name
with open(img_path,'wb') as fp:
fp.write(img_data)
print(img_name,"下载成功")
|