Python 使用 Beautiful Soup 进行数据抓取 - 从 href 中获取数据
Python Data Scraping with Beautiful Soup - geting Data from within a href
我是 Python 的新手,刚开始了解 Beautiful Soup。
所以我遇到了这个问题:我需要从活动公司获取数据,特别是联系人数据。他们有这个主要的 tables,其中包含所有参与者的姓名和他们的位置。但是要获取联系人数据(phone、电子邮件),您需要按 table 中的每个公司名称,它会打开新的 window,其中包含所有附加信息。我正在寻找一种从 href 获取该信息并将其与主 table.
中的数据组合的方法
所以我可以获得 table 和所有 href:
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen
test_url = "https://standconstruction.messe-duesseldorf.de/vis/v1/en/hallindex/1.09?oid=2656&lang=2"
test_data = urlopen(test_url)
test_html = test_data.read()
test_data.close()
page_soup = soup(test_html, "html.parser")
test_table = page_soup.findAll("div", {"class": "exh-table-col"})
print(test_table)
结果我得到了所有 table 并且有这种信息(一行的例子),包括名称、地址和 href:
<a class="flush" href="/vis/v1/en/exhibitors/aluminium2020.2661781?oid=2656&lang=2">
<h2 class="exh-table-item__name" itemprop="name">Aerospace Engineering Equipment (Suzhou) Co LTD</h2>
</a>
</div>, <div class="exh-table-col exh-table-col--address">
<span class=""><i class="fa fa-map-marker"></i> <span class="link-fix--text">Hall 9 / G57</span></span>
这就是我的问题开始的地方,我不知道如何从 href 获取附加数据并将其与主要数据结合起来。
我将非常感谢任何可能的解决方案或至少提示,我在哪里可以找到一个。
更新问题:
我需要一个 Table 其中包含以下列的信息:
1.Name; 2.Hall; 3.PDF; 4.Phone; 5.Email。
如果您手动收集数据 - 要获取 Phone 和电子邮件,您需要单击相应的 link 才能显示。
我想知道是否有办法从 Link 中导出 Phone 和电子邮件,并使用 Python.
将它们添加到前 3 列
import requests
from bs4 import BeautifulSoup
import pandas as pd
from time import sleep
params = {
"oid": "2656",
"lang": "2"
}
def main(url):
with requests.Session() as req:
r = req.get(url, params=params)
soup = BeautifulSoup(r.content, 'html.parser')
target = soup.select("div.exh-table-item")
names = [name.h2.text for name in target]
hall = [hall.span.text.strip() for hall in target]
pdf = [pdf.select_one("a.color--darkest")['href'] for pdf in target]
links = [f"{url[:46]}{link.a['href']}" for link in target]
phones = []
emails = []
for num, link in enumerate(links):
print(f"Extracting {num +1} of {len(links)}")
r = req.get(link)
soup = BeautifulSoup(r.content, 'html.parser')
goal = soup.select_one("div[class^=push--bottom]")
try:
phone = goal.select_one("span[itemprop=telephone]").text
except:
phone = "N/A"
try:
email = goal.select_one("a[itemprop=email]").text
except:
email = "N/A"
emails.append(email)
phones.append(phone)
sleep(1)
df = pd.DataFrame(list(zip(names, hall, pdf, phones, emails)), columns=[
"Name", "Hall", "PDF", "Phone", "Email"])
print(df)
df.to_csv("data.csv", index=False)
main("https://standconstruction.messe-duesseldorf.de/vis/v1/en/hallindex/1.09")
输出:View Online
我是 Python 的新手,刚开始了解 Beautiful Soup。 所以我遇到了这个问题:我需要从活动公司获取数据,特别是联系人数据。他们有这个主要的 tables,其中包含所有参与者的姓名和他们的位置。但是要获取联系人数据(phone、电子邮件),您需要按 table 中的每个公司名称,它会打开新的 window,其中包含所有附加信息。我正在寻找一种从 href 获取该信息并将其与主 table.
中的数据组合的方法所以我可以获得 table 和所有 href:
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen
test_url = "https://standconstruction.messe-duesseldorf.de/vis/v1/en/hallindex/1.09?oid=2656&lang=2"
test_data = urlopen(test_url)
test_html = test_data.read()
test_data.close()
page_soup = soup(test_html, "html.parser")
test_table = page_soup.findAll("div", {"class": "exh-table-col"})
print(test_table)
结果我得到了所有 table 并且有这种信息(一行的例子),包括名称、地址和 href:
<a class="flush" href="/vis/v1/en/exhibitors/aluminium2020.2661781?oid=2656&lang=2">
<h2 class="exh-table-item__name" itemprop="name">Aerospace Engineering Equipment (Suzhou) Co LTD</h2>
</a>
</div>, <div class="exh-table-col exh-table-col--address">
<span class=""><i class="fa fa-map-marker"></i> <span class="link-fix--text">Hall 9 / G57</span></span>
这就是我的问题开始的地方,我不知道如何从 href 获取附加数据并将其与主要数据结合起来。
我将非常感谢任何可能的解决方案或至少提示,我在哪里可以找到一个。
更新问题: 我需要一个 Table 其中包含以下列的信息: 1.Name; 2.Hall; 3.PDF; 4.Phone; 5.Email。
如果您手动收集数据 - 要获取 Phone 和电子邮件,您需要单击相应的 link 才能显示。 我想知道是否有办法从 Link 中导出 Phone 和电子邮件,并使用 Python.
将它们添加到前 3 列import requests
from bs4 import BeautifulSoup
import pandas as pd
from time import sleep
params = {
"oid": "2656",
"lang": "2"
}
def main(url):
with requests.Session() as req:
r = req.get(url, params=params)
soup = BeautifulSoup(r.content, 'html.parser')
target = soup.select("div.exh-table-item")
names = [name.h2.text for name in target]
hall = [hall.span.text.strip() for hall in target]
pdf = [pdf.select_one("a.color--darkest")['href'] for pdf in target]
links = [f"{url[:46]}{link.a['href']}" for link in target]
phones = []
emails = []
for num, link in enumerate(links):
print(f"Extracting {num +1} of {len(links)}")
r = req.get(link)
soup = BeautifulSoup(r.content, 'html.parser')
goal = soup.select_one("div[class^=push--bottom]")
try:
phone = goal.select_one("span[itemprop=telephone]").text
except:
phone = "N/A"
try:
email = goal.select_one("a[itemprop=email]").text
except:
email = "N/A"
emails.append(email)
phones.append(phone)
sleep(1)
df = pd.DataFrame(list(zip(names, hall, pdf, phones, emails)), columns=[
"Name", "Hall", "PDF", "Phone", "Email"])
print(df)
df.to_csv("data.csv", index=False)
main("https://standconstruction.messe-duesseldorf.de/vis/v1/en/hallindex/1.09")
输出:View Online