【发布时间】:2017-03-31 09:35:08
【问题描述】:
当我将我的 Scrapy 运行到 Amazon 时,我在重定向的 URL 中收到了缺少方案的错误。如何确保每个重定向 URL 都有 http?
2016-11-17 07:16:22 [scrapy] ERROR: Spider error processing <GET https://www.amazon.com/b/ref=lp_3610851_ln_1?node=3752871&ie=UTF8&qid=1479333096> (referer: None)
Traceback (most recent call last):
File "D:\Kerja\HIT\PYTHON~1\<project_name>\<project_name>\lib\site-packages\scrapy\utils\defer.py", line 102, in iter_errback
yield next(it)
File "D:\Kerja\HIT\PYTHON~1\<project_name>\<project_name>\lib\site-packages\scrapy\spidermiddlewares\offsite.py", line 29, in process_spider_output
for x in result:
File "D:\Kerja\HIT\PYTHON~1\<project_name>\<project_name>\lib\site-packages\scrapy\spidermiddlewares\referer.py", line 22, in <genexpr>
return (_set_referer(r) for r in result or ())
File "D:\Kerja\HIT\PYTHON~1\<project_name>\<project_name>\lib\site-packages\scrapy\spidermiddlewares\urllength.py", line 37, in <genexpr>
return (r for r in result or () if _filter(r))
File "D:\Kerja\HIT\PYTHON~1\<project_name>\<project_name>\lib\site-packages\scrapy\spidermiddlewares\depth.py", line 58, in <genexpr>
return (r for r in result or () if _filter(r))
File "D:\Kerja\HIT\Python Projects\<project_name>\<project_name>\<project_name>\<project_name>\spiders\amazon.py", line 133, in parse
yield scrapy.Request(url, callback=self.parse_items, meta=response.meta)
File "D:\Kerja\HIT\PYTHON~1\<project_name>\<project_name>\lib\site-packages\scrapy\http\request\__init__.py", line 25, in __init__
self._set_url(url)
File "D:\Kerja\HIT\PYTHON~1\<project_name>\<project_name>\lib\site-packages\scrapy\http\request\__init__.py", line 57, in _set_url
raise ValueError('Missing scheme in request url: %s' % self._url)
ValueError: Missing scheme in request url: /gp/slredirect/picassoRedirect.html/ref=pa_sp_btf_browse_lawngarden_sr_pg1_1?ie=UTF8&adId=A00535191JPLEGR67F8IR&url=https%3A%2F%2Fwww.amazon.com%2FRock-Solid-Supplement-Flowering-Hydroponic%2Fdp%2FB00YBHBKP2%2Fref%3Dlp_3752871_1_25%2F161-3753912-4487915%3Fs%3Dlawn-garden%26ie%3DUTF8%26qid%3D1479341778%26sr%3D1-25-spons%26psc%3D1&qualifier=1479341778&id=6512557339213691&widgetName=sp_btf_browse
更新
我查看了 Scrapy 中的基本重定向中间件,发现它已经包含以下内容:
location = safe_url_string(response.headers['location'])
redirected_url = urljoin(request.url, location)
所以从逻辑上讲,它应该已经修复了重定向 URL。为什么我的重定向 URL 仍然损坏?
更新
我已经在收益中使用了urljoin。
def parse(self, response):
for url in response.xpath(
'//div[@id="mainResults"]//a[h2/@data-attribute]/@href'
).extract():
yield scrapy.Request(response.urljoin(url), callback=self.parse_items, meta=response.meta)
【问题讨论】:
-
检查url是否以
http开头并手动添加。 -
明明没有
http,但是不知道怎么修改redirect url -
您在
yield scrapy.Request(url, callback=self.parse_items, meta=response.meta)中使用的是url吗?所以你可以访问这个url并且你可以检查它。 -
不是那个初始网址,而是“重定向”网址
-
我在错误消息中看不到任何
redirecturl - 只有您在yield scrapy.Request(url, callback=self.parse_items, meta=response.meta)中使用的url在第133 行的文件<project_name>\<project_name>\amazon.py中。打印它以查看您有什么.
标签: python url redirect scrapy url-redirection