【问题标题】:Specific a href crawling by beautiful soup in pythonpython中美汤爬取的具体a href
【发布时间】:2020-09-11 23:47:29
【问题描述】:

我正在努力学习beautifulsoup。

在网站中,它具有相同的a href,但结果不同。

比如我的代码的结果是:

0001545654

6798

0001459640

发送

0001269765

加拿大

0001456527

加拿大

0001001379

GA

我只想带数字

数字的 URL = a href="/cgi-bin/browse-edgar?action=getcompany&CIK=0001545654&owner=exclude&count=40&hidefilings=0">0001545654

区域的 URL = a href="/cgi-bin/browse-edgar?action=getcompany&State=HI&owner=exclude&count=40&hidefilings=0">HI

我只想带CIK!

有什么办法只带CIK(0001545654)吗?

from selenium import webdriver
from bs4 import BeautifulSoup
from urllib.request import urlopen

url = 'https://www.sec.gov/cgi-bin/browse-edgar?company=a&owner=exclude&action=getcompany'
page = BeautifulSoup(urlopen(url), 'html.parser')

CIK = page.find('table', 'tableFile2').find_all('a')

#print(CIK)
for i in CIK:
    print(i.get_text())

【问题讨论】:

    标签: python url beautifulsoup web-crawler google-crawlers


    【解决方案1】:

    最简单的解决方案可能是过滤您的结果,以便其中只有有效的整数:

    CIK = [i for i in CIK if str(i.get_text()).isnumeric()]
    

    或者,您可以改进 BeautifulSoup 解析以仅获取每行的第一项:

    CIK = [e.find_all('a')[0] for e in page.find('table', 'tableFile2').find_all('tr')]
    

    【讨论】:

      猜你喜欢
      • 2018-07-31
      • 2017-09-11
      • 1970-01-01
      • 2016-01-07
      • 2022-08-19
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多