您必须使用新的机械化实例打开新链接:
import mechanize
from bs4 import BeautifulSoup
i=0
url = "http://www.miamidade.gov/water/bill_app/bill_expired.asp"
br = mechanize.Browser()
br1 = mechanize.Browser()
br.open(url)
br.select_form(name="frm")
br["AcctId"] = '8428995632'
br.submit()
for link in br.links(url_regex="ID="):
print link
billurl = "http://www.miamidade.gov/water/bill_app/"+link.url
billdata = br1.open(billurl)
html = billdata.read()
soup1 = BeautifulSoup(html)
print soup1('font')[0].text[11:]
虽然我在IndexError: list index out of range
上print soup1('font')[0].text[11:]
。检查您是否正确地报废了网页(当我打开脚本输出的链接时,网页为空)
输出:
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842891990247', text='08-21-2013', tag='a', attrs=[('href', 'BillDetail.asp?ID=842891990247')])
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842892230704', text='07-22-2013', tag='a', attrs=[('href', 'BillDetail.asp?ID=842892230704')])
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842890786086', text='06-20-2013', tag='a', attrs=[('href', 'BillDetail.asp?ID=842890786086')])
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842892747030', text='05-22-2013', tag='a', attrs=[('href', 'BillDetail.asp?ID=842892747030')])
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842899532902', text='04-22-2013', tag='a', attrs=[('href', 'BillDetail.asp?ID=842899532902')])
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842899515711', text='03-21-2013', tag='a', attrs=[('href', 'BillDetail.asp?ID=842899515711')])
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842899587533', text='03-05-2013', tag='a', attrs=[('href', 'BillDetail.asp?ID=842899587533')])
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842899585097', text='01-29-2013', tag='a', attrs=[('href', 'BillDetail.asp?ID=842899585097')])
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842899536357', text='12-20-2012', tag='a', attrs=[('href', 'BillDetail.asp?ID=842899536357')])
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842899543531', text='11-26-2012', tag='a', attrs=[('href', 'BillDetail.asp?ID=842899543531')])
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842899524946', text='10-25-2012', tag='a', attrs=[('href', 'BillDetail.asp?ID=842899524946')])
Link(base_url='http://www.miamidade.gov/water/bill_app/BillHistoryLookup.Asp', url='BillDetail.asp?ID=842899578848', text='09-21-2012', tag='a', attrs=[('href', 'BillDetail.asp?ID=842899578848')])