gpt4 book ai didi

Python Mechanize : UnboundLocalError: local variable 'f' referenced before assignment

转载 作者:行者123 更新时间:2023-12-04 16:21:10 25 4
gpt4 key购买 nike

我是 python 的初学者,我正在尝试使用 mechanize 模块进行网页抓取,但由于某种原因,它一直告诉我我引用了局部变量 'f',即使我没有这样的变量。

这是我当前的整个脚本:

import mechanize
br = mechanize.Browser()
br.open('https://hac.friscoisd.org/HomeAccess/Account/LogOn?ReturnUrl=%2fhomeaccess%2f')

如果我使用完全相同的代码并将链接替换为“ https://stackoverflow.com” ',它可以工作,但是使用我需要使用的链接,我收到错误消息:
Exception has occurred: UnboundLocalError
local variable 'f' referenced before assignment
File "C:\Users\colin\MyPythonScripts\mechanizeGPA.py", line 3, in <module>
br.open('https://hac.friscoisd.org/HomeAccess/Account/LogOn?ReturnUrl=%2fhomeaccess%2f')

请帮忙!

最佳答案

您可以在发送到图书馆之前尝试取消引用网址

>>> import urllib
>>> url = "https://hac.friscoisd.org/HomeAccess/Account/LogOn?ReturnUrl=%2fhomeaccess%2f"
>>> url = urllib.parse.unquote('https://hac.friscoisd.org/HomeAccess/Account/LogOn?ReturnUrl=%2fhomeaccess%2f')
>>> url
'https://hac.friscoisd.org/HomeAccess/Account/LogOn?ReturnUrl=/homeaccess/'

关于Python Mechanize : UnboundLocalError: local variable 'f' referenced before assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319295/

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