【问题标题】:Scraping pdfs from a webpage从网页中抓取 pdf
【发布时间】:2020-03-28 16:06:14
【问题描述】:

我想从丹麦公司注册 (csv register) 下载给定公司的所有财务报告。一个例子可能是 Chr。汉森控股在以下链接:

https://datacvr.virk.dk/data/visenhed?enhedstype=virksomhed&id=28318677&soeg=chr%20hansen&type=undefined&language=da

具体来说,我想下载“Regnskaber”选项卡下的所有 PDF(=财务报告)。我以前没有使用 Python 进行网页抓取的经验。我尝试使用 BeautifulSoup,但鉴于我不存在的经验,我无法从响应中找到正确的搜索方式。

以下是我尝试过的,但没有打印任何数据(即它没有找到任何 pdf)。

from urllib.parse import urljoin
from bs4 import BeautifulSoup

web_page = "https://datacvr.virk.dk/data/visenhed? 
enhedstype=virksomhed&id=28318677&soeg=chr%20hansen&type=undefined&language=da"

response = requests.get(web_page)
soup = BeautifulSoup(response.text)
soup.findAll('accordion-toggle')

for link in soup.select("a[href$='.pdf']"):
    print(link['href'].split('/')[-1])

我们将不胜感激所有帮助和指导。

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    你应该使用 select 而不是 findAll

    from urllib.parse import urljoin
    from bs4 import BeautifulSoup
    
    web_page = "https://datacvr.virk.dk/data/visenhed? 
    enhedstype=virksomhed&id=28318677&soeg=chr%20hansen&type=undefined&language=da"
    
    response = requests.get(web_page)
    soup = BeautifulSoup(response.text, 'lxml')
    pdfs = soup.select('div[id="accordion-Regnskaber-og-nogletal"] a[data-type="PDF"]')
    
    for link in pdfs:
        print(link['href'].split('/')[-1])
    

    【讨论】:

    • 这是非常精确和有用的,并且做到了,我可以继续我的项目。非常感谢。
    猜你喜欢
    • 2018-03-13
    • 2019-02-16
    • 1970-01-01
    • 2020-06-18
    • 2019-07-22
    • 1970-01-01
    • 2014-10-30
    • 2017-07-04
    相关资源
    最近更新 更多