【问题标题】:Python Mechanize: UnboundLocalError: local variable 'f' referenced before assignmentPython Mechanize:UnboundLocalError:分配前引用的局部变量'f'
【发布时间】:2019-01-23 02:41:04
【问题描述】:

我是 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')

请帮忙!

【问题讨论】:

  • Python 机械化早已死去。您想为此使用请求。
  • 感谢您的意见,我会检查一下

标签: python mechanize


【解决方案1】:

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

>>> 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/'

【讨论】:

    猜你喜欢
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多