从多个迭代页面返回数据

时间:2018-11-29 22:36:38

标签: python python-3.x loops web-scraping

以下代码从首页返回许可证。同样有1075页,如何从所有页面返回许可证号? [如果可能的话,我也想返回名称字段]

输入-我在网站的“ {Agent / Broker Last Name:”(代理商/经纪人的姓氏:)”字段中输入%号-http://mbsweblist.fsco.gov.on.ca/agents.aspx

这似乎是页码的操作-a href =“ javascript:__ doPostBack('ctl00 $ ctl00 $ MainPlaceHolder $ Content4 $ resultsGrid','Page $ 2')”> 2

谢谢,感谢您的帮助

import requests
from bs4 import BeautifulSoup

def get_result_page_ontario(name):
r = requests.post("http://mbsweblist.fsco.gov.on.ca/agents.aspx",
data={
    'ctl00$ctl00$MainPlaceHolder$Content4$bkmbname:': '%',
    '_EVENTTARGET': '',
    '__EVENTARGUMENT': '',
    '__LASTFOCUS': '',
    '__VIEWSTATE': '/wEPDwULLTEwMzk1Nzk2NDAPZBYCZg9kFgJmD2QWBAIBD2QWAgIBDxYCHgRUZXh0BcUHPGxpbmsgcmVsPSdzdHlsZXNoZWV0JyBocmVmPSdodHRwOi8vd3d3LmZzY28uZ292Lm9uLmNhLy9TdHlsZSBMaWJyYXJ5L0ZTQ08vSW50ZXJuZXQvQ1NTL21hc3Rlci5jc3MnIHR5cGU9J3RleHQvY3NzJyBtZWRpYT0nc2NyZWVuJyAvPjxsaW5rIHJlbD0nc3R5bGVzaGVldCcgaHJlZj0naHR0cDovL3d3dy5mc2NvLmdvdi5vbi5jYS8vU3R5bGUgTGlicmFyeS9GU0NPL0ludGVybmV0L0NTUy9jaGFuZ2VtZS5jc3MnIHR5cGU9J3RleHQvY3NzJyBtZWRpYT0nc2NyZWVuJy8+PCEtLVtpZiBsdGUgSUUgNl0+PGxpbmsgcmVsPSdzdHlsZXNoZWV0JyBocmVmPSdodHRwOi8vd3d3LmZzY28uZ292Lm9uLmNhLy9TdHlsZSBMaWJyYXJ5L0ZTQ08vSW50ZXJuZXQvQ1NTL2llNi5jc3MnIHR5cGU9J3RleHQvY3NzJ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Qo6YdBnxt7SdQEcL98iN0RCUjhr/FmBpke28iIjJEQtlWEoAG7jfIg+',
    'ctl00$ctl00$MainPlaceHolder$Content4$searchoption': 'Agents or Broker',
    'ctl00$ctl00$MainPlaceHolder$Content4$bkmbno': '',
    'ctl00$ctl00$MainPlaceHolder$Content4$bkmbname': name,
    'ctl00$ctl00$MainPlaceHolder$Content4$agbkcity': '',
    'ctl00$ctl00$MainPlaceHolder$Content4$srButton': 'Search',
    'ctl00$ctl00$hLocal': 'en',
    'ctl00$ctl00$hIsWide': '0'
})
return r.text

def parse_result_page_ontario(page):
soup = BeautifulSoup(page, 'html.parser')
allA = soup.find_all('a', href=True)
licenses = []
for a in allA:
    if('ShowLicence.aspx' in a['href']):
        licenses.append(a.text)
return licenses
name = '%'
page = get_result_page_ontario(name)
licenses = parse_result_page_ontario(page)
print (licenses)

0 个答案:

没有答案