import requests
import re
import os
import time
def download_pic(html):
''' 获取图片下载链接 '''
# 设置正则表达式,查找图片链接
pat1 = r'"objURL":"(.*?)",'
pic_url = re.findall(pat1, html)
# 创建保存路径文件夹
path = '百度图片'
if not os.path.exists(path):
os.makedirs(path)
# 下载图片
for url in pic_url:
r = requests.get(url)
name = url[-10:]
filename = path + '/' + name
print("正在下载图片:%s" % name)
try:
with open(filename, 'wb') as f:
f.write(r.content)
time.sleep(1)
except Exception as e:
print("图片下载出错!", e)
def get_html(url):
# 设置代理头
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36",
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image
python爬虫-通过关键字查询下载百度高清图片
最新推荐文章于 2023-08-14 17:26:41 发布