gpt4 book ai didi

Python 值错误 : too many values to unpack for crawler

转载 作者:行者123 更新时间:2023-11-30 22:33:39 26 4
gpt4 key购买 nike

我正在尝试运行我在网上找到的抓取工具,但收到一个 ValueError:太多值无法在这行代码上解压

 k, v = piece.split("=")

该行是该函数的一部分

def format_url(url):
# make sure URLs aren't relative, and strip unnecssary query args
u = urlparse(url)

scheme = u.scheme or "https"
host = u.netloc or "www.amazon.com"
path = u.path

if not u.query:
query = ""
else:
query = "?"
for piece in u.query.split("&"):
k, v = piece.split("=")
if k in settings.allowed_params:
query += "{k}={v}&".format(**locals())
query = query[:-1]

return "{scheme}://{host}{path}{query}".format(**locals())

如果您有任何意见,我们将不胜感激,谢谢。

最佳答案

您可以使用urlparse.parse_qs函数,而不是自己解析url:

>>> from urlparse import urlparse, parse_qs
>>> URL = 'https://someurl.com/with/query_string?i=main&mode=front&sid=12ab&enc=+Hello'
>>> parsed_url = urlparse(URL)
>>> parse_qs(parsed_url.query)
{'i': ['main'], 'enc': [' Hello '], 'mode': ['front'], 'sid': ['12ab']}

( source )

关于Python 值错误 : too many values to unpack for crawler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45091474/

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