Mechanize:类型错误:Nonetype对象不支持项目分配

时间:2015-04-09 03:39:13

标签: python mechanize

我正在尝试使用Mechanize来获取一些数据并且我得到错误" Nonetype对象不支持项目分配"。我复制下面使用的代码。

import mechanize

url = "http://www.tropicos.org"

br = mechanize.Browser()

br.form["ct100_MainContentPlaceHolder_acNameControl_textBox"] = "poa    annua"
response = br.submit()
print response.read()

2 个答案:

答案 0 :(得分:1)

您的问题是,在访问表单之前,您没有在open上致电br。因此,请尝试以下方法:

import mechanize

url = "http://www.tropicos.org"

br = mechanize.Browser()
br.open(url) #RIGHT HERE
br.form["ct100_MainContentPlaceHolder_acNameControl_textBox"] = "poa    annua"
response = br.submit()
print response.read()

它应该有用。

答案 1 :(得分:0)

尝试添加useragent和数字表单。

您的代码将如下所示:

import mechanize

useragents = [('User-agent',
           'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)')]

url = "http://www.tropicos.org"
br = mechanize.Browser()
br.addheaders = [('User-agent', random.choice(useragents))]
site = br.open(url)
br.select_form(nr=0)   
br.form["ct100_MainContentPlaceHolder_acNameControl_textBox"] = "poa    annua"
response = br.submit()
print response.read()

通常将select_form设置为0 有些情况下它的隐藏