【问题标题】:Python: Beautiful soup to get textPython:获取文本的美丽汤
【发布时间】:2019-05-16 10:34:41
【问题描述】:

我正在尝试获取a href 下的链接以及下一个<td scope = "raw"> 中可用的文本

我试过了

url = "https://www.sec.gov/Archives/edgar/data/1491829/0001171520-19-000171-index.htm"
records = []
for link in soup.find_all('a'):
    Name = link.text
    Links = link.get('href')
    records.append((Name, Links))

但是这给了我eps8453.htm 作为文本,因为这是标签<a href> 下的文本。有什么方法可以在标签<td scope = "raw"> 旁边的标签<a href> 中查找文本,即“10-K”

请帮忙!

【问题讨论】:

    标签: python-3.x beautifulsoup lxml


    【解决方案1】:

    在表内<a> 标签之后使用 find_next <td> 标签。

    import requests
    from bs4 import BeautifulSoup
    
    url = "https://www.sec.gov/Archives/edgar/data/1491829/0001171520-19-000171-index.htm"
    html=requests.get(url).text
    soup=BeautifulSoup(html,'html.parser')
    records = []
    for link in soup.find('table', class_='tableFile').find_all('a'):
        Name = link.text
    
        Links = link.get('href')
        text=link.find_next('td').contents[0]
        print(Name,text)
        records.append((Name, Links,text))
    

    输出:

    eps8453.htm 10-K
    ex31-1.htm EX-31.1
    ex31-2.htm EX-31.2
    ex32-1.htm EX-32.1
    yu-logo.jpg GRAPHIC
    yu_sig.jpg GRAPHIC
    0001171520-19-000171.txt 
    

     

    【讨论】:

      猜你喜欢
      • 2021-05-24
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 2018-05-08
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多