项目
博客
文档
归档
资源链接
关于我
项目
博客
文档
归档
资源链接
关于我
Python爬虫虎牙获取图片(源码)
2021-06-27
·
yuan
·
原创
·
源码
·
本文共 104个字,预计阅读需要 1分钟。
```python import requests from lxml import etree #解析工具 # 1. 找到抓取目标位置 url = 'https://www.huya.com/g/1663' # 2. 获取数据 response = requests.get(url) html = etree.HTML(response.text) image_list = html.xpath('//img[@class="pic"]') for image_data in image_list: image_url =image_data.xpath('./@data-original')[0] image_url = image_url.split('?')[0] if(image_url.startswith('https://')): image_url = image_url else: image_url = 'https:'+image_url print(image_url) image_name = image_data.xpath('./@alt')[0] image = requests.get(image_url) with open('./image/%s.jpg' % image_name, 'wb') as file: file.write(image.content) print('<<%s>>下载成功...' % image_name) ```