python-3.x - 如何使用 requests() 和 open() 下载所有二进制文件(图像)?
问题描述
当我尝试从一个 URL 下载图像时,代码有效,但是当我尝试另一个 URL 时,它不起作用。这不起作用。它只创建文件名。
# This Doesn't Work.
import requests
url = 'https://ryanspressurewashing.com/wp-content/uploads/2017/06/metal-
roof-after-pressure-washing.jpg'
r = requests.get(url, stream=True)
with open('image3.jpg', 'wb') as my_file:
# Read by 4KB chunks
for byte_chunk in r.iter_content(chunk_size=4096):
my_file.write(byte_chunk)
# This Works?
import requests
url = 'http://www.webscrapingfordatascience.com/files/kitten.jpg'
r = requests.get(url, stream=True)
with open('image.jpg', 'wb') as my_file:
# Read by 4KB chunks
for byte_chunk in r.iter_content(chunk_size=4096):
my_file.write(byte_chunk)
解决方案
不同的门户可能有不同的安全系统来阻止脚本/机器人。
当您image3.jpg
在文本编辑器中打开时,您会看到
<head>
<title>Not Acceptable!</title>
</head>
<body>
<h1>Not Acceptable!</h1>
<p>An appropriate representation of the requested resource could not be found on this server.
This error was generated by Mod_Security.</p>
</body>
</html>
某些服务器可能需要正确headerse
的 , cookies
,session-id
等才能访问数据。
此门户需要正确的标题user-agent
import requests
url = 'https://ryanspressurewashing.com/wp-content/uploads/2017/06/metal-roof-after-pressure-washing.jpg'
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0'
}
r = requests.get(url, stream=True, headers=headers)
with open('image3.jpg', 'wb') as my_file:
# Read by 4KB chunks
for byte_chunk in r.iter_content(chunk_size=4096):
my_file.write(byte_chunk)
requests
作为默认用途user-agent: python-requests/2.21.0
,门户网站可以轻松识别脚本并阻止它。
您可以使用https://httpbin.org/get
import requests
r = requests.get('https://httpbin.org/get')
print(r.text)
结果:
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"origin": "83.23.39.165, 83.23.39.165",
"url": "https://httpbin.org/get"
}
在httpbin.org上查看更多功能
推荐阅读
- .net - 如何在不更新文件上传字段的情况下更新实体
- sql - 将一个表定义为高于另一个表的优先级,然后根据表优先级选择属性
- snakemake - 我可以为snakemake 定义一个默认配置文件吗?
- r - Tidymodels - 在 XGBoost 分类模型中使用 PCA 输出
- node.js - 通过对象相等测试查找 Mongo 文档
- terraform - Terraform:错误:提供程序配置不存在
- outlook - 从 Azure AD 获取 Outlook 通讯录
- java - 为什么我的方法 pickANumberPro() 不起作用
- flutter - 注解必须是 const 变量引用或 const 构造函数调用
- java - 无法从我的电脑连接到 docker 中的 redis sentinal