虎牙直播虎牙直播下载电脑版下载

发布时间: 2023-04-17 05:23 阅读: 文章来源:转载

作用:本代码是在python3.7,pycharm编辑器里演示通过python的requests,etree,request.urlretrieve下载虎牙直播平台的交友分类的美女图片

操作步骤如下:

1、解析虎牙的美女图片网络地址,如下图所示:

找到所有的图片都是在img标签下,class属性为pic的标签里data-original的属性值

2、在pycharm里编写代码,代码如下:

from lxml import etreeimport requestsfrom urllib import request#从urllib导入request,用urlretrive下载数据import time#导入time模块,用sleep控制爬去速度、def hello(n):#定义函数hello,参数是nfor i in range(n):#遍历数组里的range(n)的数据print("hello world")#打印helloworldhello(5)#调用hello函数,参数5,即打印4遍helloworlddef huyaspider():#定义爬去虎牙的数据的爬虫函数huyaspiderurl="https://www.huya.com/g/4079"#设置请求网址url为虎牙的交友分类的网址headers={"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"}#设置请求头response=requests.get(url,headers=headers)#通过headers设置伪装浏览器对网站url进行get请求,并将请求的响应结果赋值给response变量data=etree.HTML(response.text)#对响应的文本信息进行解析并赋值给data变量,这句很关键print("data:",data)imgurl_list=data.xpath(‘//img[@class="pic"]‘)#获取data里的img标签,并且img标签的class属性值是pic,并赋值给imgurl_listprint(imgurl_list)#打印列表元素imgurl_listfor imgurl in imgurl_list:img=imgurl.xpath(‘./@data-original‘)[0]#获取imgurl的data-original属性值,并赋值给imgimg=img.split(‘?‘)[0]#根据问号进行切割,将切割的前面部分赋值给imgname=imgurl.xpath(‘./@alt‘)[0]##获取imgurl的alt属性值,并赋值给name,作为图片名称print(name,img)request.urlretrieve(img,‘F:/明星照片/xiaojiejie/‘+name+‘.jpg‘)#通过request库的urltrieve函数下载图片,下载的图片网路地址是img,图片保存位置是F:/明星照片/xiaojiejie/‘+name+‘.jpghuyaspider()#调用huyaspider函数

代码运行结果如下:

hello world

hello world

hello world

hello world

hello world

data:

[, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ]

情書FM-电台的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1019/71/aec9b19740fca541afb9f3a7bd32e8_4079_1569741127.jpg

9c-私密领域的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1028/d6/78090755a02d1dfdff7689f8529791_4079_1574761904.jpg

DY-上帝的宠儿的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1031/1d/b1e33edaa752cf75f1b2e542712703_4079_1575007578.jpg

8090-知音FM的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1041/ef/ba8498a4fbec17749cecca14327e3c_4079_1575889322.jpg

Top-恋爱的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1047/9c/24b77bfff9be248e362bf2e17b1242_4079_1574912064.jpg

98-一起鬼混的直播 https://huyaimg.msstatic.com/cdnimage/anchorpost/1071/37/059cf96b0672714ed8192f3659111f_4079_1554631506.jpg

Dae-勿忘初心的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1099/33/1205fde5a8fc5809e3167a6bf057cb_4079_1575903811.jpg

金水-你最珍贵的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1037/9d/dcae162e722fe433f31f2def84cd7a_4079_1573553003.jpg

Myth-恋爱季节的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1012/25/aa8e889fa98e1ff07b5b75883e2589_4079_1573558901.jpg

【娱加】万里挑一的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1092/b5/be18c5b19debf764850da2d8d86975_4079_1564037795.jpg

【娱加】私人订制的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1052/eb/e24acb17d7ccf1061e78bf50e0ae4f_4079_1559571129.jpg

Dae-爸爸我甜的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1090/d0/ef9ea7ded8969536bab31fed145239_4079_1575899785.jpg

繁星-怦然心动的直播 https://huyaimg.msstatic.com/cdnimage/anchorpost/1082/5a/6534e16e8b40ca93738d1f047de817_4079_1544076598.jpg

Dae-没有名字的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1015/cc/e7a4b72372741a7cafc30e50b72fa1_4079_1574137390.jpg

花漾-沈阳萌妹的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1071/0a/b82eb665cf76f290d7df28a5dda0e2_4079_1573749816.jpg

Top-花花世界的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1081/f4/4dcad96cb7651fab490c30780a439d_4079_1556281729.jpg

大鱼-纸短情长的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1089/87/789054d4c17d2a972473d0ccff7812_4079_1576680155.jpg

中国蓝-心灵驿站的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1074/8e/dfa722b1302c9f4233d7450645a703_4079_1557753863.jpg

花漾-杭州娇妹的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1054/6d/f366a32aaaeff451497e727ed6598c_4079_1562917535.jpg

仙境-壹号公馆的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1014/9e/6e34ed4d7601290eb9301611807583_4079_1555395970.jpg

Top-连麦吗的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1053/ff/4842542b64a956a07724307f65812f_4079_1563874669.jpg

鑫-唇唇欲动的直播 https://anchorpost.msstatic.com/cdnimage/anchorpost/1013/0f/b13715d95c997e6e95574ecfcc6381_4079_1575642390.jpg

保存到本机的图片如下图所示:

•••展开全文
相关文章