python 爬取 baidu 图片
pip install fake-useragent
以下是一段爬取百度图片的脚本:
# -*- coding: utf-8 -*-
"""
Filename : spider_image_baidu.py
Description: Spider - get images from baidu
"""
import requests
import os
import re
import urllib3
urllib3.disable_warnings()
import time
import random
from fake_useragent import UserAgent
def get_images_from_baidu(keyword, page_num, save_dir):
# UA 伪装:当前爬取信息伪装成浏览器
# 将 User-Agent 封装到一个字典中
# 【(网页右键 → 审查元素)或者 F12】 → 【Network】 → 【Ctrl+R】 → 左边选一项,右边在 【Response Hearders】 里查找
# 请求的 url
url = 'https://image.baidu.com/search/acjson?'
n = 0
for pn in range(0, 30 * page_num, 30):
headers = {'User-Agent': UserAgent().random}
print(pn)
# 请求参数
param = {'tn': 'resultjson_com',
# 'logid': '7603311155072595725',
'ipn': 'rj',
'ct': 201326592,
'is': '',
'fp': 'result',
'queryWord': keyword,
'cl': 2,
'lm': -1,
'ie': 'utf-8',
'oe': 'utf-8',
'adpicid': '',
'st': -1,
'z': '',
'ic': '',
'hd': '',
'latest': '',
'copyright': '',
'word': keyword,
's': '',
'se': '',
'tab': '',
'width': '',
'height': '',
'face': 0,
'istype': 2,
'qc': '',
'nc': '1',
'fr': '',
'expermode': '',
'force': '',
'cg': '', # 这个参数没公开,但是不可少
'pn': pn, # 显示:30-60-90
'rn': '30', # 每页显示 30 条
'gsm': '1e',
'1618827096642': ''
}
request = requests.get(url=url, headers=headers, params=param, verify=False)
time.sleep(3)
if request.status_code == 200:
print('Request success.')
request.encoding = 'utf-8'
# 正则方式提取图片链接
html = request.text
image_url_list = re.findall('"thumbURL":"(.*?)",', html, re.S)
print(image_url_list)
# # 换一种方式
# request_dict = request.json()
# info_list = request_dict['data']
# # 看它的值最后多了一个,删除掉
# info_list.pop()
# image_url_list = []
# for info in info_list:
# image_url_list.append(info['thumbURL'])
if not os.path.exists(save_dir):
os.makedirs(save_dir)
for image_url in image_url_list:
image_data = requests.get(url=image_url, headers=headers, verify=False).content
with open(os.path.join(save_dir, f'{n:06d}.jpg'), 'wb') as fp:
fp.write(image_data)
n = n + 1
if __name__ == '__main__':
keyword = '穿背心的人'
save_dir = keyword
page_num = 100
get_images_from_baidu(keyword, page_num, save_dir)
print('Get images finished.')
如果遇到链接断开,请修改如下两行:
n = 4339
for pn in range(1200, 30 * page_num, 30):
n 修改为当前的 n,1200 为当前的页,就可以继续爬了。直到解析不出来图片。
参考:
【1】爬取百度图片
【2】报错requests.exceptions.ConnectionError: HTTPSConnectionPool(host=‘xxx’, port=443): Max re
【3】TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。