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(),减缓爬取速度,但考虑到爬取数据较多,运行时 ...
随机推荐
- python3GUI--200行代码写一个上课点名程序(附源码)
@ 目录 一.准备工作 1.Tkinter 2.PIL 二.预览 1.启动 2.开始点名-顺序点名 3.开始点名-随机点名 4.手动加载人名单 5.开始点名-顺序点名-Pyqt5版本 6.人名单格式 ...
- PVE联网及更换国内源
一.PVE联网 第一次安装PVE,正常情况下PVE的IP是在我们上网的网段的.没有网络有可能是没有配置DNS服务器地址或DNS地址是软路由网关地址.解决方法有3种: 1:设置DHCP自动获取网络地址和 ...
- windows 获取USB,发现安卓设备,转载自www.jb51.net/article/164456.htm
转载 作者:jgw2008 import win32com.client def CheckDev(): wmi = win32com.client.GetObject ("winmgmts ...
- js对象常用的方法
1. Object.assign()方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象,它将返回目标对象. 语法: Object.assign(target, ...sources) ...
- 关于 indy Idhttp Post数据抛异常 connection closed Gracefully
delphi 使用indy -idHttp 控件post 数据时 会报connection closed Gracefully这个异常的问题. 网上找了很多方法最多的就是 修改: MyHttp.Req ...
- hhtp协议和html标签分类css
HTTP协议四大特性: 1基于请求响应 2 基于tcp/ip协议之上的应用层协议 3 无状态 不能保存用户信息 4 无链接,短链接 二 get和post的区别? 1 get 不安全,get请求没有请求 ...
- 遇到 https://packetlife.net/
"I have never let my schooling interfere with my education." --Mark Twain
- OpenStack组件说明
1.版本对应组件 2.Juno版本的核心组件
- Windhill获取团队角色、用户
//获取容器团队里的用户和角色,也可以获取容器团队里某一角色的用户 WTContainer pContainer = project.getContainer(); if (pContainer in ...
- python之自动化连连看脚本-第一关不动-小记
(如想转载,请联系博主或贴上本博地址) 仅供学习python之用,勿用做商业用途.运行环境为1920*1080屏幕,python3.7,win7,谷歌浏览器版本 75.0.3770.100. 参考ht ...