python - 在 python 中使用 BS 抓取页面仅捕获 splitColumn 的第一列

标签 python web-scraping beautifulsoup

我正在尝试抓取 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/

相关文章:

python - 无法访问biopython成对对齐中的各个对齐字符串

android - 从网站提取特定文本 block 到 Android 应用程序

python - HTTP header - 请求 - Python

python - Beautifulsoup:findAll 递归不起作用

python - 使用 BeautifulSoup4 从 HTML 中提取字段

python-3.x - 如何从网络上抓取某一类属性的所有子级?

python - 在python中查找递归调用的级别

python - 替换子列表中的项目而不展平

python - 如何在python中将所有参数转换为字典

python - 如何加载 BeautifulSoup 页面解析器?