python爬虫---虎牙直播封面采集
代码:
import requests from lxml import etree # html解析库 source = requests.get("https://www.huya.com/g/4079").text html = etree.HTML(source) # 获取所有的<img class='pic'>的标签 pic_list = html.xpath("//img[@class='pic']") for pic in pic_list: # 小图地址 pic_src = pic.xpath("./@data-original")[0] # 大图地址 big_pic_src = pic.xpath("./@data-original")[0].split("?")[0] # 获取图片名称 name = pic.xpath("./@alt")[0] # 将图片写入本地 image = requests.get(big_pic_src) # 写入本地 with open("./美女/%s.jpg" % name, "wb") as file: file.write(image.content) print("<%s>保存成功!" % name)