Python 类型错误

标签 python mechanize typeerror

我是编程新手。我正在尝试使用 Python 和 Mechanize 下载比赛结果。我试图通过创建 for 来循环浏览页面。循环我在下面包含了相关代码。
当我运行它时,我收到以下错误:

类型错误:% 不支持的操作数类型:'instance' 和 'int'

为什么我不能在 URL 中使用变量 x?

import mechanize

br = mechanize.Browser()

for x in range(1, 3): 
    page = br.open('http://results.chicagomarathon.com/2013/?page=%d&event=MAR') % (x)
    print page.read()

最佳答案

问题在于,当您应该格式化 url 时,您正试图用整数格式化 Mechanize 的响应。

以下代码格式化 url,然后尝试检索它:

import mechanize

br = mechanize.Browser()

for x in range(1, 3):
     url = 'http://results.chicagomarathon.com/2013/?page=%d&event=MAR' % (x)
     page = br.open(url)
     print page.read()

关于Python 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20833990/

相关文章:

python - texts_to_sequences() 缺少 1 个必需的位置参数 : 'texts'

Python - Mechanize 的请求 header

python - Mechanize 和 Google App Engine

python - 为什么我的代码中会出现 Control NotFoundError?

python - 使用复杂列表定义类时出现类型错误

python - 如何将数据从 excel 列提取到 Python 列表中?

python - 如何在Python中从原始数据和列中查找索引?

javascript - (已关闭)未捕获的类型错误 : Cannot call method 'download' of undefined

javascript - TypeError: Functionname 不是 HTMLButtonElement.onClick 中的函数

python - 从两列字符串创建 pandas 列包含 NA