Нет смысла использовать здесь Beautifulsoup, так как вы можете получать данные непосредственно из API. Для начала вам нужно узнать, сколько существует организаций, чтобы вы могли использовать это в запросе. Затем, взяв 'WebsiteKey'
или организацию id
, вы можете перебирать API, чтобы получать электронные письма. Вы можете хранить в словаре, таблице, распечатать, и т. Д. c. Не уверен, что вы действительно хотите в качестве вывода.
import requests
import pandas as pd
url = 'https://blueprint.uchicago.edu/api/discovery/search/organizations'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}
payload = {
'orderBy[0]': 'UpperName asc',
'top': '',
'filter':'',
'query':'' ,
'skip': '0'}
data = requests.get(url, headers=headers, params=payload).json()
totalCount = data['@odata.count']
payload = {
'orderBy[0]': 'UpperName asc',
'top': '%s' %totalCount,
'filter':'',
'query':'' ,
'skip': '0'}
data = requests.get(url, headers=headers, params=payload).json()
organizations = {}
for each in data['value']:
organizations[each['Name']] = {'id':each['Id'], 'WebsiteKey':each['WebsiteKey']}
emails = {}
for name, each in organizations.items():
websiteKey = each['WebsiteKey']
org_id = each['id']
url = 'https://blueprint.uchicago.edu/api/discovery/organization/bykey/%s' %websiteKey
data = requests.get(url, headers=headers).json()
emails[name] = data['email']
print('%-70s: %s' %(name, data['email']))
df = pd.DataFrame(list(zip(emails.keys(), emails.values())), columns=['Organization','Email'])
df.to_csv('file.csv', index=False)
Вывод:
{'A Cappella Council': 'uchicagoacappella@gmail.com', 'ACLU University of Chicago Law Chapter': 'dhbabrams@uchicago.edu', 'Active Minds at the University of Chicago': 'activemindsuchicago@gmail.com', 'African and Caribbean Student Association': 'cvleito@uchicago.edu', 'Aikido Kokikai': 'nahmadc@uchicago.edu', 'Alpha Kappa Psi': 'edwardchang@uchicago.edu', 'Alpha Phi Omega': 'uchi.apo.president@gmail.com', 'American Civil Liberties Union at University of Chicago': 'acluboard@lists.uchicago.edu', 'American Constitution Society': 'acs@law.uchicago.edu', 'American Medical Student Association': None, 'American Red Cross of University of Chicago': 'rkhouri@uchicago.edu', 'Amnesty International': 'eckere@uchicago.edu', 'Animal Legal Defense Fund - The University of Chicago Law School': 'ntschepik@uchicago.edu', 'Animal Welfare Society': 'petrucci@uchicago.edu', 'Anthropology Students Association': 'frevelolarotta@uchicago.edu', 'Apsara': 'uchicagoapsara@gmail.com', 'Arab Student Association': 'malakarafa@uchicago.edu', ...}