gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:36:00 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com