还在学习过程中请多多指教
代码
import requests # 向网站发起网络请求
from lxml import etree #
def huya_live():
url = 'https://www.huya.com/g/4079'#要请求的网络连接
headers = {
'user-agent': ''#模拟浏览器操作
}
response = requests.get(url=url, headers=headers)请求回的值
data_text = response.text#保存数据
# print(data_text)
data = etree.HTML(data_text)
girls = data.xpath('//img[@class="pic"]')
# print(girls)
for girl in girls:
img_url = girl.xpath('./@data-original')[0] #获得图片连接
img_url = img_url.split('?')[0] #去除图片缩小的限制
name = girl.xpath('./@alt')[0]
image = requests.get(url=img_url)
with open('./Girl/%s.jpg' % name, 'wb') as jpg:#保存图片的位置
jpg.write(image.content)
print("<%s>下载完成!" % name)
if __name__ =='__main__':#调用
huya_live()