爬虫实例(爬取照片)以虎牙为例
开始之前点赞,投币加关注哦
开发环境:Python3.7
开发软件:PyCharm Edu
第一步:导入第三方库(模块)
# 导入第三方库
import requests
import re
导入方法
一:在终端输入
pip3 install requests
二:使用PyCharm Edu
文件 --> 设置 --> 项目Python --> Python解释器 --> 下方加号+ --> 搜索requests和re --> 安装包
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210227110142918.png#pic_center
re不是第三方库,是Python自带的就不用麻烦了
2.模拟游览器发起请求
# 1.1 地址: 信息
url = "https://www.huya.com/g/2168#cate-1-2609" # 选择图片所在的网站
# 1.2 User-Agent获取
# 网页 -> 右键点击检查 -> Network -> Name -> 任意选择一行 -> Headers -> 复制 User-Agent
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"
}
3.获取响应内容
# 2.1 发起请求: urllib requests(第三方库)
data = requests.get(url, headers=header)
print(data) # 如果不是200, 全是错
print(data.text)
# 这是data的输出
<Response [200]>
# 这是data.text的一部分输出(太长了)
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="ie6" lang="zh-cmn-Hans"> <![endif]-->
<!--[if IE 7]> <html class="ie7" lang="zh-cmn-Hans"> <![endif]-->
<!--[if IE 8]> <html class="ie8" lang="zh-cmn-Hans"> <![endif]-->
<!--[if IE 9]> <html class="ie9" lang="zh-cmn-Hans"> <![endif]-->
<!--[if gt IE 9]><!--> <html lang="zh-cmn-Hans"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>美女直播_美女秀场直播间_美女主播_颜值直播大厅_虎牙直播</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="Keywords" content="颜值直播,美女直播,美女秀场直播
"/>
<meta name="Description" content="虎牙直播提供海量的颜值美女直播内容,众多美女秀场直播间全天不间断直播,与美女主播一起零距离互动,全新的美女直播尽在虎牙直播。
"/>
<link rel='canonical' href='https://www.huya.com/g/2168' />
<link rel="stylesheet" href="https://a.msstatic.com/huya/main3/common/headerStyle_5352d.css">
<link rel="stylesheet" href="https://a.msstatic.com/huya/main3/app/game-live_4d318.css">
<style id="J_restrictedLiveStyle">
.game-live-item[data-lp="1099531752746"], .game-live-item[data-lp="1099531752747"], .game-live-item[data-lp="1099531752748"], .game-live-item[data-lp="1099531752749"], .game-live-item[data-lp="1099531752750"], .game-live-item[data-lp="1099531752751"], .game-live-item[data-lp="1524434072"], .game-live-item[data-lp="1394575552"], .game-live-item[data-lp="1423787856"], .game-live-item[data-lp="1423782061"], .game-live-item[data-lp="1423782074"], .game-live-item[data-lp="1423782052"], .game-live-item[data-lp="1423787883"], .game-live-item[data-lp="1423782104"], .game-live-item[data-lp="1423787836"], .game-live-item[data-lp="1524418101"], .game-live-item[data-lp="1423782038"], .game-live-item[data-lp="1423782096"], .game-live-item[data-lp="1423782053"], .game-live-item[data-lp="1524418078"], .game-live-item[data-lp="1524418081"], .game-live-item[data-lp="1394575539"], .game-live-item[data-lp="1423782044"], .game-live-item[data-lp="1394575535"], .game-live-item[data-lp="1423782104"], .game-live-item[data-lp="1199558100755"], .game-live-item[data-lp="1199558257305"], .game-live-item[data-lp="1394565211"], .game-live-item[data-lp="1423787819"], .game-live-item[data-lp="1388473933"], .game-live-item[data-lp="1388457186"], .game-live-item[data-lp="1099531752754"], .game-live-item[data-lp="1857682899"], .game-live-item[data-lp="2286799170"], .game-live-item[data-lp="1199512045291"], .game-live-item[data-lp="1199526558405"], .game-live-item[data-lp="1851300403"], .game-live-item[data-lp="1199560287662"], .game-live-item[data-lp="1423782047"], .game-live-item[data-lp="119956224454"], .game-live-item[data-lp="1199562247002"], .game-live-item[data-lp="1199562242900"], .game-live-item[data-lp="1199562248271"] {
display: none;
}
.game-live-item[data-lp="1099531752746"], .game-live-item[data-lp="1099531752747"], .game-live-item[data-lp="1099531752748"], .game-live-item[data-lp="1099531752749"], .game-live-item[data-lp="1099531752750"], .game-live-item[data-lp="1099531752751"], .game-live-item[data-lp="1524434072"], .game-live-item[data-lp="1394575552"], .game-live-item[data-lp="1423787856"], .game-live-item[data-lp="1423782061"], .game-live-item[data-lp="1423782074"], .game-live-item[data-lp="1423782052"], .game-live-item[data-lp="1423787883"], .game-live-item[data-lp="1423782104"], .game-live-item[data-lp="1423787836"], .game-live-item[data-lp="1524418101"], .game-live-item[data-lp="1423782038"], .game-live-item[data-lp="1423782096"], .game-live-item[data-lp="1423782053"], .game-live-item[data-lp="1524418078"], .game-live-item[data-lp="1524418081"], .game-live-item[data-lp="1394575539"], .game-live-item[data-lp="1423782044"], .game-live-item[data-lp="1394575535"], .game-live-item[data-lp="1423782104"], .game-live-item[data-lp="1199558100755"], .game-live-item[data-lp="1199558257305"], .game-live-item[data-lp="1394565211"], .game-live-item[data-lp="1423787819"], .game-live-item[data-lp="1388473933"], .game-live-item[data-lp="1388457186"], .game-live-item[data-lp="1099531752754"], .game-live-item[data-lp="1857682899"], .game-live-item[data-lp="2286799170"], .game-live-item[data-lp="1199512045291"], .game-live-item[data-lp="1199526558405"], .game-live-item[data-lp="1851300403"], .game-live-item[data-lp="1199560287662"], .game-live-item[data-lp="1423782047"], .game-live-item[data-lp="119956224454"], .game-live-item[data-lp="1199562247002"], .game-live-item[data-lp="1199562242900"], .game-live-item[data-lp="1199562248271"] {
display: none;
}
4.解析内容
# re findall("解析规则", 解析文件)
# 解析规则: BS4 xpath 正则表达式: 要简单
# () 获取当前位置内容 . 表示任意字符 * 0次或者多次匹配 ? 满足规则情况下尽可能少的匹配
img = re.findall('<img class="pic" data-original="(.*?)"', data.text)
print(img)
name = re.findall('data-default-img="338x190" alt="(.*?)"', data.text)
print(name)
# 这是img输出的,都是图片的路径(一部分的)
['https://anchorpost.msstatic.com/cdnimage/anchorpost/1092/c1/1590496b04897499c911f776d04cd9_2168_1609902921.jpg?imageview/4/0/w/338/h/190/blur/1',
'https://anchorpost.msstatic.com/cdnimage/anchorpost/1051/9d/6e80b4c9dd829160959b06ae87b652_2168_1609824507.jpg?imageview/4/0/w/338/h/190/blur/1',
'https://anchorpost.msstatic.com/cdnimage/anchorpost/1023/8b/b66fba5b43ffcccdd623e01e4a9138_2168_1591800022.jpg?imageview/4/0/w/338/h/190/blur/1',
'https://anchorpost.msstatic.com/cdnimage/anchorpost/1012/c9/63d681759b6cc968177d1c32fd7033_2168_1609374434.jpg?imageview/4/0/w/338/h/190/blur/1',
'https://anchorpost.msstatic.com/cdnimage/anchorpost/1052/02/35b606fdd746e860c5cac153ab7961_2168_1608387251.jpg?imageview/4/0/w/338/h/190/blur/1',
'https://anchorpost.msstatic.com/cdnimage/anchorpost/1054/ac/2297275a4a7e739044d7c86bde3bac_2168_1612262404.jpg?imageview/4/0/w/338/h/190/blur/1',
'https://anchorpost.msstatic.com/cdnimage/anchorpost/1044/79/4c16008b00d2fd230ce4357e9c3c84_2168_1612151081.jpg?imageview/4/0/w/338/h/190/blur/1']
# 这是name的输出,这些是图片的名称(一部分的)
['赢城、亦可的直播', '正恒YZ-阿离的直播', '赢城、雨鹿的直播', '正恒-芝麻呀的直播',
'清风-露美的直播', '赢城-玲baby的直播', 'sc-漪洛的直播', '乐动-小巫巫的直播',
'起风丶小蝴蝶的直播', '清风-小雨儿的直播', '童话、幽默【Q徒】的直播',
'话社、萝莉【小老虎】的直播', '扶摇-容容的直播', '听风-十月【铭记】的直播',
'小象-F级别小九【9299】的直播', '羲和仙子【超白】的直播', '赢城丶乖乖的直播']
5.保存
num = 0
for i, n in zip(img, name):
try:
num += 1
resq = requests.get(i, n)
# with open("保存地址") as file:
with open("./img/"+str(num)+"."+str(n)+".jpg", "wb") as file:
file.write(resq.content)
print(n + "打印完成!")
except:
print(n + "打印失败!")
print("\n全部打印完成!")
# 这是打印完成后的提示(一部分)可能有一部分的是打印失败问题不大!所以这里加了一个try...except...
赢城、亦可的直播打印完成!
正恒YZ-阿离的直播打印完成!
赢城、雨鹿的直播打印完成!
正恒-芝麻呀的直播打印完成!
小象-F级别小九【9299】的直播打印完成!
赢城-玲baby的直播打印完成!
sc-漪洛的直播打印完成!
乐动-小巫巫的直播打印完成!
起风丶小蝴蝶的直播打印完成!
清风-小雨儿的直播打印完成!
扶摇-容容的直播打印完成!
童话、幽默【Q徒】的直播打印完成!
话社、萝莉【小老虎】的直播打印完成!
听风-十月【铭记】的直播打印完成!
清风-蜜兔的直播打印完成!
羲和仙子【超白】的直播打印完成!
赢城丶乖乖的直播打印完成!
......
全部打印完成!
如果要保存图片,把图片保存到指定的路径下,提前建好文件夹
这样你就可以获取照片了
这就很nice!
觉得代码乱,没关系!我把代码全部整理在下面
import requests
import re
# 1.模拟浏览器发起请求
# 1.1 地址: 信息
url = "https://www.huya.com/g/2168#cate-1-2609"
# 1.2 User-Agent获取
# 网页 -> 右键检查 -> Network -> Name -> 任意选择一行 -> Headers -> 复制 User-Agent
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"
}
# 2.获取响应内容
# 2.1 发起请求: requests(第三方库)
data = requests.get(url, headers=header)
# print(data)
# print(data.text) # 如果不是200, 全是错
# 3.解析内容
# re findall("解析规则", 解析文件)
# 解析规则: BS4 xpath 正则表达式: 要简单
# () 获取当前位置内容 . 表示任意字符 * 0次或者多次匹配 ? 满足规则情况下尽可能少的匹配
img = re.findall('<img class="pic" data-original="(.*?)"', data.text)
# print(img)
name = re.findall('data-default-img="338x190" alt="(.*?)"', data.text)
# print(name)
# print(list(zip(img, name)))
# 4.保存内容
num = 0
for i, n in zip(img, name):
try:
num += 1
resq = requests.get(i, n)
# with open("保存地址") as file:
with open("./img/"+str(num)+"."+str(n)+".jpg", "wb") as file:
file.write(resq.content)
print(n + "打印完成!")
except:
print(n + "打印失败!")
print("\n全部打印完成!")
在此感谢B站Ride_Python的教学视频
https://www.bilibili.com/video/BV1Nr4y1A7kz
记得点赞,投币加关注哦!