我正在尝试抓取 this 的最后一部分在Python中通过BeautifulSoup进行页面。
我想检索底部列出的所有公司。此外,公司按字母顺序排序,标题以“A-F”开头的公司出现在第一个选项卡下,然后是“G-N”出现在第二个选项卡下,依此类推。您必须单击选项卡才能显示名称,因此我将循环遍历不同的“名称页面”并应用相同的代码。
但是,我在检索单个页面的所有名称时遇到了麻烦。 当查看名为“A-F”的公司时,我只能检索表第一列的名称。
我的代码是:
from bs4 import BeautifulSoup as Soup
import requests
incl_page_url = "https://www.triodos.com/en/investment-management/socially-
responsible-investment/sustainable-investment-universe/companies-atmf1/"
page = requests.get(incl_page_url)
soup = Soup(page.content, "html.parser")
for header in soup.find("h2").next_siblings:
try:
for a in header.childGenerator():
if str(type(a)) == "<class 'bs4.element.NavigableString'>":
print(str(a))
except:
pass
通过运行可以看出,我只从第一列中获取名称。 非常感谢任何帮助。
最佳答案
尝试一下并告诉我这不是您想要的:
from bs4 import BeautifulSoup
import requests
incl_page_url = "https://www.triodos.com/en/investment-management/socially-responsible-investment/sustainable-investment-universe/companies-atmf1/"
page = requests.get(incl_page_url).text
soup = BeautifulSoup(page, "lxml")
for items in soup.select(".splitColumn p"):
title = '\n'.join([item for item in items.strings])
print(title)
结果:
3iGroup
8point3 Energy Partners
A
ABN AMRO
Accell Group
Accsys Technologies
Achmea
Acuity Brands
Adecco
Adidas
Adobe Systems
关于python - 在 python 中使用 BS 抓取页面仅捕获 splitColumn 的第一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47133737/