[英]Specific a href crawling by beautiful soup in python
我正在尝试学习 beautifulsoup。
在网站中,它具有相同的href,但结果不同。
例如,我的代码的结果是:
0001545654
6798
你好
0001459640
德克萨斯州
0001269765
加利福尼亚州
0001456527
加利福尼亚州
0001001379
遗传算法
我只想带数字
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())
最简单的解决方案可能是过滤您的结果,以便其中只有有效的整数:
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')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.