python - 网页抓取错误 - 网站所有者的错误:网站密钥的域无效
问题描述
我试图获取此 URL 的内容 - https://www.zillow.com/homedetails/131-Avenida-Dr-Berkeley-CA-94708/24844204_zpid/ 我使用了scrapy。这是我的代码。
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'https://www.zillow.com/homedetails/131-Avenida-Dr-Berkeley-CA-94708/24844204_zpid/',
]
def parse(self, response):
filename = 'test.html'
with open(filename, 'wb') as f:
f.write(response.body)
self.log('Saved file %s' % filename)
我打开了抓取的数据(test.html),我得到了这个内容。 我试图找到解决方案并尝试了这个 -站点所有者错误:站点密钥的域无效 但它没有解决我的问题。
解决方案
首先,尝试这种方法,看看它是否有效:
Headerz = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"pragma": "no-cache",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "cross-site",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36",
}
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'https://www.zillow.com/homedetails/131-Avenida-Dr-Berkeley-CA-94708/24844204_zpid/',
]
def start_requests(self):
yield scrapy.Request(start_urls[0], callback=self.parse, headers=Headerz)
def parse(self, response):
filename = 'test.html'
with open(filename, 'wb') as f:
f.write(response.body)
self.log('Saved file %s' % filename)
我们在普通浏览器中看不到输出的原因是我们没有使用正确的标头,否则这些标头总是由浏览器发送。
您需要按照上述代码中的说明添加标头,或者通过在 settings.py 中更新它们来添加标头。
更好的方法是使用“旋转代理”存储库和“旋转用户代理”存储库。
推荐阅读
- python - python中的数据表示差异
- html - 无法用鼠标单击列表中的第一个单选按钮,但可以使用向上和向下箭头键选择它
- java - 是否可以从对象数组中删除索引?
- python - 使用 python 将 [abc] 转换为 [a,b,c]
- django - DRF 序列化程序错误:AttributeError:“FeedPostSerializer”对象没有属性“auth_user”
- css - Webpack CSS 输出总是最小化
- python - 带条件的左外连接
- c# - 使用参数化数据绑定查询,查找最近“x”天内列为真的行数
- c# - 如何为积分创建图表
- php - 如何创建一个下拉子菜单并将其与之前单个菜单使用的数组相关联?