【问题标题】:How to webscrape the CSV files that are hidden behind the button?如何网页抓取隐藏在按钮后面的 CSV 文件?
【发布时间】:2021-09-11 19:59:49
【问题描述】:

【问题讨论】:

  • 我想我有个主意。 PDF 文件和 CSV 链接都共享唯一的 7 位数字。我可以获得所有感兴趣的唯一 7 位数字的列表,我可以使用这些数字来查询/下载 CSV 文件。让我们看看情况如何。

标签: python csv web-scraping


【解决方案1】:

试试:

import requests
from bs4 import BeautifulSoup


main_link = "https://www12.statcan.gc.ca/census-recensement/2016/dp-pd/prof/search-recherche/lst/results-resultats.cfm?Lang=E&TABID=1&G=1&Geo1=&Code1=&Geo2=&Code2=&GEOCODE=35&type=0"
soup = BeautifulSoup(requests.get(main_link).content, "html.parser")

for a in soup.select('details a[href*="page.cfm"]'):
    link = a["href"]
    link = link.replace(
        "../../details/page.cfm",
        "https://www12.statcan.gc.ca/census-recensement/2016/dp-pd/prof/details/download-telecharger/current-actuelle.cfm",
    )
    link += "&FILETYPE=CSV"

    print(a.get_text(strip=True))
    print(link)
    print()

打印:

Abitibi 70 (Indian reserve)
https://www12.statcan.gc.ca/census-recensement/2016/dp-pd/prof/details/download-telecharger/current-actuelle.cfm?Lang=E&Geo1=CSD&Code1=3556033&Geo2=PR&Code2=35&SearchText=Abitibi%2070&SearchType=Begins&SearchPR=01&B1=All&GeoLevel=PR&GeoCode=3556033&TABID=1&type=0&FILETYPE=CSV

Addington Highlands (Township)
https://www12.statcan.gc.ca/census-recensement/2016/dp-pd/prof/details/download-telecharger/current-actuelle.cfm?Lang=E&Geo1=CSD&Code1=3511035&Geo2=PR&Code2=35&SearchText=Addington%20Highlands&SearchType=Begins&SearchPR=01&B1=All&GeoLevel=PR&GeoCode=3511035&TABID=1&type=0&FILETYPE=CSV

Adelaide-Metcalfe (Township)
https://www12.statcan.gc.ca/census-recensement/2016/dp-pd/prof/details/download-telecharger/current-actuelle.cfm?Lang=E&Geo1=CSD&Code1=3539047&Geo2=PR&Code2=35&SearchText=Adelaide-Metcalfe&SearchType=Begins&SearchPR=01&B1=All&GeoLevel=PR&GeoCode=3539047&TABID=1&type=0&FILETYPE=CSV

Adjala-Tosorontio (Township)
https://www12.statcan.gc.ca/census-recensement/2016/dp-pd/prof/details/download-telecharger/current-actuelle.cfm?Lang=E&Geo1=CSD&Code1=3543003&Geo2=PR&Code2=35&SearchText=Adjala-Tosorontio&SearchType=Begins&SearchPR=01&B1=All&GeoLevel=PR&GeoCode=3543003&TABID=1&type=0&FILETYPE=CSV

Admaston/Bromley (Township)
https://www12.statcan.gc.ca/census-recensement/2016/dp-pd/prof/details/download-telecharger/current-actuelle.cfm?Lang=E&Geo1=CSD&Code1=3547043&Geo2=PR&Code2=35&SearchText=Admaston/Bromley&SearchType=Begins&SearchPR=01&B1=All&GeoLevel=PR&GeoCode=3547043&TABID=1&type=0&FILETYPE=CSV

...and so on.

【讨论】:

    【解决方案2】:

    所有第一个子链接的链接都列为<li> 元素。在我看来,解析初始链接的 HTML 文本并将第一个子链接存储在列表中,然后使用 selenium 导航到它们(并下载 CSV)将是可行的方法。

    【讨论】:

      猜你喜欢
      • 2016-08-26
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 2018-08-20
      • 2015-09-17
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多