python - 如何从 url 中删除查询?

标签 python url scrapy web-crawler

我正在使用 scrapy 抓取一个网站,该网站似乎将随机值附加到每个 URL 末尾的查询字符串中。这会将爬行变成一种无限循环。

如何使 scrapy 忽略 URL 的查询字符串部分?

最佳答案

参见 urllib.urlparse

示例代码:

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/

相关文章:

python - 如何使 Pyre-check 库与 numpy 一起工作?

python - 两个矩阵的元素之间的组合

php - 带有 ñ 或重音符号的友好 Url 问题字符串

Python Scrapy 代理几轮后就死了?

python - 不是 "\u": How to Unescape Unicode in JSON?

python - 解析由标签分隔的部分

Python3 为 PyPi 准备包,不包括子模块

python - 来自字典的pylab中的条形图

java - 从 URL 获取域名/主机名的最快方法是什么?

java - URLClassLoader.getResources ("") (空资源名称) 没有给出 jars 的根