我的脚本正在尝试从网站上抓取存储在文本文件中的 80000 个 ID。当我使用单个 o/p 运行代码时,它运行良好,但是当我将所有输入放入循环中时,我收到错误。
我的代码:
from bs4 import BeautifulSoup
import mechanize
br = mechanize.Browser()
response = br.open("https://www.matsugov.us/myproperty")
for form in br.forms():
if form.attrs.get('name') == 'frmSearch':
br.form = form
break
br.form['ddlType']=["taxid"]
with open("names.txt") as ins:
tx = ins.read().splitlines()
for x in tx:
br['txtParm'] = x
req = br.submit().read()
soup = BeautifulSoup(req, 'html.parser')
table = soup.find('td', {'class': 'Grid_5'})
for row in table:
print row
错误:
AttributeError: mechanize._mechanize.Browser 实例没有属性 __setitem__ (也许您忘记了 .select_form()?)
最佳答案
你把循环放错了!将其放在 mechanize 浏览器下方,然后在末尾放置一个 try catch。它会起作用的。一旦你尝试过并且有效,请发表评论。
关于python - 脚本在抓取多个值时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47383106/