如何将名称列表放入for循环中并搜索其许可证ID和有效期?
我要通过网络剪贴该网站“ http://mbsweblist.fsco.gov.on.ca/agents.aspx”。我有代理商的姓氏。使用姓氏搜索后,它会返回许可证ID,这是一个超链接单击,可将您带到其许可证信息(例如到期日期)的另一页。
我的输入将是.csv文件中的代理名称的列表,输出应是具有代理名称,许可证编号和到期日期的.csv或excel文件。我宁愿不使用硒。请让我知道是否有办法?以下代码一次搜索并返回一个代理的输出。谢谢
列表图片:enter image description here
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:': 'crossley',
'_EVENTTARGET': '',
'__EVENTARGUMENT': '',
'__LASTFOCUS': '',
'__VIEWSTATE': '/wEPDwULLTEwMzk1Nzk2NDAPZBYCZg9kFgJmD2QWBAIBD2QWAgIBDxYCHgRUZXh0BcUHPGxpbmsgcmVsPSdzdHlsZXNoZWV0JyBocmVmPSdodHRwOi8vd3d3LmZzY28uZ292Lm9uLmNhLy9TdHlsZSBMaWJyYXJ5L0ZTQ08vSW50ZXJuZXQvQ1NTL21hc3Rlci5jc3MnIHR5cGU9J3RleHQvY3NzJyBtZWRpYT0nc2NyZWVuJyAvPjxsaW5rIHJlbD0nc3R5bGVzaGVldCcgaHJlZj0naHR0cDovL3d3dy5mc2NvLmdvdi5vbi5jYS8vU3R5bGUgTGlicmFyeS9GU0NPL0ludGVybmV0L0NTUy9jaGFuZ2VtZS5jc3MnIHR5cGU9J3RleHQvY3NzJyBtZWRpYT0nc2NyZWVuJy8+PCEtLVtpZiBsdGUgSUUgNl0+PGxpbmsgcmVsPSdzdHlsZXNoZWV0JyBocmVmPSdodHRwOi8vd3d3LmZzY28uZ292Lm9uLmNhLy9TdHlsZSBMaWJyYXJ5L0ZTQ08vSW50ZXJuZXQvQ1NTL2llNi5jc3MnIHR5cGU9J3RleHQvY3NzJyBtZWRpYT0nc2NyZWVuJ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CUjhr/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
def parse_license_page_ontario(license):
r = requests.get("http://mbsweblist.fsco.gov.on.ca/ShowLicence.aspx?" +
license + "~")
soup = BeautifulSoup(r.text, 'html.parser')
return soup.find("span", {"id":
"MainPlaceHolder_Content4_cragexpiry"}).text
name = 'crossley'
page = get_result_page_ontario(name)
licenses = parse_result_page_ontario(page)
for l in licenses:
print(l)
print(parse_license_page_ontario(l))
答案 0 :(得分:1)
在这种情况下,您只需要遍历input.csv
就可以将其定义为names
import pandas as pd
names = pd.DataFrame({"First Name":["Grant", "Orlando",
"Adam", "Levan"],
"Last Name": ["Morris", "Fantini",
"Crossley", "Sichinava"]})
out = []
for _, name in names.iterrows():
page = get_result_page_ontario(name["Last Name"])
licenses = parse_result_page_ontario(page)
for l in licenses:
out.append([name["First Name"],
name["Last Name"],
l, parse_license_page_ontario(l)])
out = pd.DataFrame(out, columns=["First Name",
"Last Name",
"License",
"Expires"])
out.to_csv("output.csv", index=False)