python 爬取美女图片的练习 主要使用 xpath 定位获取 图片的链接 本次练习使用到os库 ,lmxl库 , requests库
import requests from lxml import etree import os if name == ‘main’: headers = { ‘user-agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36’ }
url = r"https://www.tupianzj.com/meinv/mm/jurumeinv/"
respond= requests.get(url=url,headers=headers) #发起请求
respond.encoding = 'gbk'
res = respond.text
tree = etree.HTML(res)
title_list = tree.xpath('//*[@id="container"]/div/div/div[2]/div[2]/div[2]/dl/dd/ul/li') # 元素定位
if not os.path.exists('./picture'):
os.mkdir('./picture')
for i in title_list:
res = i.xpath('./a/img/@src')[0]
a = i.xpath('./a/img/@alt')[0] + 'jpg'
img_data = requests.get(url =res,headers=headers).content
img_path = 'picture/'+ a
with open(img_path,'wb') as fp:
fp.write(img_data)
print(img_data,'下载成功')
print(res,a)
|