首页 > 解决方案 > 使用 Python 抓取特定的复选框值

问题描述

我正在尝试分析此网站上的数据:网站

我想抓取几个国家,例如 BZN|PT - BZN|ES 和 BZN|RO - BZN|BG

我尝试了以下预测的TransferCapacitiesMonthAhead:

from bs4 import BeautifulSoup
import requests

page = requests.get('https://transparency.entsoe.eu/transmission-domain/r2/forecastedTransferCapacitiesMonthAhead/show')

soup = BeautifulSoup(page.text, 'html.parser')

tran_month = soup.find('table', id='dv-datatable').findAll('tr')

for price in tran_month:
    print(''.join(price.get_text("|", strip=True).split()))

但我只得到预选的国家。我怎样才能传递我的论点,以便我可以选择我想要的国家?非常感谢。

标签: web-scraping

解决方案


该代码缺少一个关键部分 - 即通知请求的参数,如导入/导出以及来自/到国家和类型。

为了解决这个问题,您可能会在下面找到一个基于您的代码构建的代码,该代码使用requests. 要运行完整的代码,您应该找出每个国家/地区的完整参数列表。

from bs4 import BeautifulSoup
import requests

payload = { # this is the dictionary whose values can be changed for the request
    'name' : '',
    'defaultValue' : 'false',
    'viewType' : 'TABLE',
    'areaType' : 'BORDER_BZN',
    'atch' : 'false',
    'dateTime.dateTime' : '01.05.2020 00:00|UTC|MONTH',
    'border.values' : 'CTY|10YPL-AREA-----S!BZN_BZN|10YPL-AREA-----S_BZN_BZN|10YDOM-CZ-DE-SKK',
    'direction.values' : ['Export', 'Import']
    }

page = requests.get('https://transparency.entsoe.eu/transmission-domain/r2/forecastedTransferCapacitiesMonthAhead/show',
                     params = payload) # GET request + parameters

soup = BeautifulSoup(page.text, 'html.parser')

tran_month = soup.find('table', id='dv-datatable').findAll('tr')

for price in tran_month: # print all values, row by row (date, export and import)
    print(price.text.strip())

推荐阅读