VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • Python爬虫实战:批量爬取百度图片

众所周知,某度本身就是最大的爬虫脚本,那么纯纯的去某个网站找壁纸,还不如去某度图片直接找,瞬间格局打开!

话不多说,直接用Python来开发一下此处资源!

开发环境 & 第三方模块

环境

解释器版本 >>> python 3.8
代码编辑器 >>> pycharm 2021.2

模块

requests  # 发送请求  第三方模块

 

键盘按住 win+r 打开搜索,输入cmd 按回车确定,弹出命令提示符窗口后,输入 pip install requests 回车即可安装。

代码展示

复制代码
import requests    
# Python源码/资料自取群 708525271
 
# 伪装
headers = {
    # 身份信息
    'Cookie': 'BDqhfp=jk%E5%B0%8F%E5%A7%90%E5%A7%90%26%26NaN-1undefined%26%260%26%261; BIDUPSID=C24C7D8E598E67C686237DEAF51F7B28; PSTM=1656683846; BAIDUID=90512AA3632B152E5F977142A71CD0B0:SL=0:NR=10:FG=1; newlogin=1; MCITY=-%3A; BDUSS=VBkUnZuOUJjVWJRSzRQVmRrOFNuVlZpOWxXMXI2MmN5eUhmdG94aElsVXJXSWhqSVFBQUFBJCQAAAAAAAAAAAEAAADRI~rzv8nE3MrHybXByzMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACvLYGMry2Bje; BDUSS_BFESS=VBkUnZuOUJjVWJRSzRQVmRrOFNuVlZpOWxXMXI2MmN5eUhmdG94aElsVXJXSWhqSVFBQUFBJCQAAAAAAAAAAAEAAADRI~rzv8nE3MrHybXByzMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACvLYGMry2Bje; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; indexPageSugList=%5B%22%E4%BB%96%E5%A6%88%E7%9A%84%20%E8%A1%A8%E6%83%85%E5%8C%85%22%2C%22%E6%87%92%20%E8%A1%A8%E6%83%85%E5%8C%85%22%2C%22%E6%88%91%E5%BE%88%E6%87%92%20%E8%A1%A8%E6%83%85%E5%8C%85%22%2C%22%E5%B0%B1%E8%BF%99%E6%A0%B7%E7%BB%93%E6%9D%9F%E4%BA%86%20%E8%A1%A8%E6%83%85%E5%8C%85%22%2C%22%E5%9C%9F%E5%BA%93%E6%9B%BC%E6%96%AF%E5%9D%A6%22%2C%22%E5%9C%9F%E5%BA%93%E6%9B%BC%E6%96%AF%E5%9D%A6%E4%B8%96%E7%95%8C%E5%9C%B0%E5%9B%BE%22%2C%22%E5%9C%9F%E5%BA%93%E6%9B%BC%E6%96%AF%E5%9D%A6%E4%B8%96%E7%95%8C%22%2C%22%E7%82%B9%E4%B8%AA%E8%B5%9E%20%E8%A1%A8%E6%83%85%E5%8C%85%22%5D; BA_HECTOR=240000240g8g058k2l848qet1hn93qn1f; ZFY=:AUZ:AsSR27Ydrc2egIjwyfTn:BiQthXJLEzDLR4IgwdOQ:C; BAIDUID_BFESS=90512AA3632B152E5F977142A71CD0B0:SL=0:NR=10:FG=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; BDRCVFR[C0p6oIjvx-c]=ddONZc2bo5mfAF9pywdpAqVuNqsus; BDRCVFR[fb3VbsUruOn]=_M5urk4djP3fA4-ILn; H_PS_PSSID=36545_37771_37778_37724_36802_37662_37538_37672_37741_26350_37786; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; BDRCVFR[Txj84yDU4nc]=mk3SLVN4HKm; BDRCVFR[tox4WRQ4-Km]=mk3SLVN4HKm; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; firstShowTip=1; PSINO=7; userFrom=www.baidu.com; ab_sr=1.0.1_ZDc4MzMwNmRiYmU4MjdiMDYyMGJlNjgzMzc0ZDU5OWRmMzg3OTk1NDY3ZjUzZGY3YWY0NGIzMzVhNzYyYzRlNzkzODc2ZmQxNDU5ZmIwZWFhNjQwYmE1MzVkMTY2YTI1YTRhNWYxOTY5MzE2YThkODJiNjQxNDk0MDYxMzBmNmNjOWMxMWUwOTI5MGUyZWU3ZmU1YjJhZjljN2ZlNzkyMg==',
    # 域名
    'Host': 'image.baidu.com',
    # 防盗链
    'Referer': 'https://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&dyTabStr=MCwzLDIsNSwxLDcsNCw2LDgsOQ%3D%3D&word=jk%E5%B0%8F%E5%A7%90%E5%A7%90',
    # 浏览器的基本信息
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}

url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=8212365409067552127&ipn=rj&ct=201326592&is=&fp=result&fr=&word=jk%E5%B0%8F%E5%A7%90%E5%A7%90&queryWord=jk%E5%B0%8F%E5%A7%90%E5%A7%90&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=©right=&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&expermode=&nojc=&isAsync=&pn=90&rn=30&gsm=5a&1668600962847='

response = requests.get(url=url, headers=headers)

json_data = response.json()     # 字典

data_list = json_data['data']
count = 0
for data in data_list[:-1]:
    ObjUrl = data['replaceUrl'][0]['ObjUrl']
    img_data = requests.get(ObjUrl).content
    with open(f'{count}.jpg', mode='wb') as f:
        f.write(img_data)
    count += 1
复制代码

 

效果展示

这难道比壁纸网站的差了吗?

最后

# 我给大家准备了一些资料,包括2022最新Python视频教程、Python电子书10个G (涵盖基础、爬虫、数据分析、web开发、机器学习、人工智能、面试题)、Python学习路线图等等,直接在这个君羊 708525271 自取即可!


出处:https://www.cnblogs.com/hahaa/p/16900946.html
 



相关教程