Python爬取pexels图片
研究Python爬虫,网上很多爬取pexels图片的案例,我下载下来运行没有成功,总量有各种各样的问题。
作为菜鸟初学者,网上的各个案例代码对我还是有不少启发作用,我用搜索引擎+chatGPT逐步对代码进行了完善。
最终运行成功。特此记录。
运行环境:Win10,Python3.10、Google Chrome111.0.5563.148(正式版本)
1 import urllib.request
2 from bs4 import BeautifulSoup
3 import os
4 import html
5 import requests
6 import urllib.parse
7
8 path = r"C:\Users\xiaochao\pexels"
9 url_lists = ['https://www.pexels.com/search/book/?page={}'.format(i) for i in range(1, 2)]
10 headers = {
11 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36",
12 "Referer": "https://www.pexels.com/",
13 "Accept-Language": "en-US,en;q=0.9",
14 }
15
16 for url in url_lists:
17 print(url)
18 req = urllib.request.Request(url, headers=headers)
19 try:
20 resp = urllib.request.urlopen(req)
21 except urllib.error.HTTPError as e:
22 print("HTTPError occurred: {}".format(e))
23 continue
24
25 html_content = resp.read().decode()
26 soup = BeautifulSoup(html_content, "html.parser")
27
28 import re
29 pattern = re.compile('"Download" href="(.*?)/?cs=', re.S)
30 matches = re.findall(pattern, html_content)
31 print(matches)
32
33 if not os.path.exists(path):
34 os.makedirs(path)
35
36 for match in matches:
37 match_cleaned = match.split('?')[0] # 去除图片URL地址最后带的“?”号。
38 print(match_cleaned) # 输出去除图片URL“?”号的地址
39 match_cleaned = html.unescape(match_cleaned) #解码 HTML 编码字符,将文件链接还原为正常的 URL 格式
40 match_cleaned = urllib.parse.unquote(match_cleaned) # 对 URL 进行进一步处理,解码URL,确保它的格式正确,包括删除多余的引号和处理特殊字符。
41 match_cleaned = urllib.parse.urljoin(url, match_cleaned) # 将相对 URL 转换为绝对 URL
42
43
44 # 按URL地址后段命名
45 filename = match_cleaned.split("/")[-1]
46 with open(os.path.join(path, filename), "wb") as f:
47 f.write(requests.get(match_cleaned).content)
Python爬取pexels图片的更多相关文章
- Python爬取谷歌街景图片
最近有个需求是要爬取街景图片,国内厂商百度高德和腾讯地图都没有开放接口,查询资料得知谷歌地图开放街景api 谷歌捷径申请key地址:https://developers.google.com/maps ...
- 利用Python爬取网页图片
最近几天,研究了一下一直很好奇的爬虫算法.这里写一下最近几天的点点心得.下面进入正文: 你可能需要的工作环境: Python 3.6官网下载 我们这里以sogou作为爬取的对象. 首先我们进入搜狗图片 ...
- Python 爬取美女图片,分目录多级存储
最近有个需求:下载https://mm.meiji2.com/网站的图片. 所以简单研究了一下爬虫. 在此整理一下结果,一为自己记录,二给后人一些方向. 爬取结果如图: 整体研究周期 2-3 天, ...
- python爬取网页图片(二)
从一个网页爬取图片已经解决,现在想要把这个用户发的图片全部爬取. 首先:先找到这个用户的发帖页面: http://www.acfun.cn/u/1094623.aspx#page=1 然后从这个页面中 ...
- 用python 爬取网页图片
import re import string import sys import os import urllib url="http://tieba.baidu.com/p/252129 ...
- python爬取网页图片
# html:网页地址 def getImg2(html): soup = BeautifulSoup(html, 'html.parser') href_regex = re.compile(r'^ ...
- python爬取百度图片
import requests import re from urllib import parse import os from threading import Thread def downlo ...
- Python 爬取图书图片和地址
#-*- coding:utf-8 -*- import xlwt import urllib import re def getHtml(url): page = urllib.urlopen(ur ...
- python爬取许多图片的代码
from bs4 import BeautifulSoup import requests import os os.makedirs('./img/', exist_ok=True) URL = & ...
- 实例学习——爬取Pexels高清图片
近来学习爬取Pexels图片时,发现书上代码会抛出ConnectionError,经查阅资料知,可能是向网页申请过于频繁被禁,可使用time.sleep(),减缓爬取速度,但考虑到爬取数据较多,运行时 ...
随机推荐
- 20181224《网络攻防技术》Exp7 网络欺诈防范
20181224<网络攻防技术>Exp7 网络欺诈防范 目录 20181224<网络攻防技术>Exp7 网络欺诈防范 相关知识点总结 ARP Spoofing IP源地址欺骗 ...
- github使用流程
前期硬件准备工作(电脑相关配置): 1.下载git软件,傻瓜式安装 https://git-scm.com/download/win 2.设置你的用户名称与邮件地址 git config --glob ...
- ddddd
项目二阶段总结 账户微服务 短信发送 1.压测发现问题 首先对短信smscomponent的send方法在test单元测试类中测试,不是真的发短信测试,可以建立请求开始和结束的时间戳来确定请求的耗时. ...
- 2.20 Q_Learning 和Sarsa 的区别
二者都是基于Qtable的算法,其中Qlearning属于off-policy,Sarsa属于on-policy. 算法伪代码: 二者主要区别是更新Qtable的方式不同:
- Windows查看CUDA版本
桌面右击,查看是否有NVIDIA控制面板 打开控制面板->帮助->系统信息->组件,可以看到CUDA版本
- MAVEN实践经验
1安装与配置 jdk: 1.6或以上 下载MAVEN3.x版本,解压后放在随便一目录,然后在系统环境变量配置MAVEN路径. 运行cmd-->输入 mvn -version 会出现maven版本 ...
- squad经验总结
啊美丽卡:M1A2 - TANKM2A3 - BLDL/M2A3M1126 - SCKMATV - RWS(电摇),ZCC(手摇)MATV(TOW) - TOW车M989 - 补给卡/运兵卡 俄军 8 ...
- Spring Cloud netty
<properties> <spring.boot.version>2.3.2.RELEASE</spring.boot.version> <spring.c ...
- hello cnb
Huawei executive Meng Wanzhou freed by Canada arrives home in China 目录 关于git merge冲突时候的想法 Git修改commi ...
- Mapper method 'org.lin.hms.dao.IndentDAO.insertIndent' has an unsupported return type: interface java.util.List
出现这种错误,说明sql语句执行成功,只是返回类型出了问题. 解决办法: mapper文件中的update,delete,insert语句是不需要设置返回类型的,它们都是默认返回一个int ,所以把返 ...