我正在使用 scrapy 抓取一个网站,该网站似乎将随机值附加到每个 URL 末尾的查询字符串中。这会将爬行变成一种无限循环。
如何使 scrapy 忽略 URL 的查询字符串部分?
最佳答案
示例代码:
from urlparse import urlparse
o = urlparse('http://url.something.com/bla.html?querystring=stuff')
url_without_query_string = o.scheme + "://" + o.netloc + o.path
示例输出:
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from urlparse import urlparse
>>> o = urlparse('http://url.something.com/bla.html?querystring=stuff')
>>> url_without_query_string = o.scheme + "://" + o.netloc + o.path
>>> print url_without_query_string
http://url.something.com/bla.html
>>>
关于python - 如何从 url 中删除查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8567171/