web-scraping - 使用 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()))
但我只得到预选的国家。我怎样才能传递我的论点,以便我可以选择我想要的国家?非常感谢。
解决方案
该代码缺少一个关键部分 - 即通知请求的参数,如导入/导出以及来自/到国家和类型。
为了解决这个问题,您可能会在下面找到一个基于您的代码构建的代码,该代码使用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())
推荐阅读
- r - 发送“未分类”变量作为函数参数?
- html - 如何在所有生成的 html 文件中自动添加 css 导入?
- c# - 如何关闭客户端连接并返回 NETMQ 中的调用?
- javascript - “无法读取 null 的属性‘innerHTML’”错误
- java - 从 Angular 项目到 jhipster 的 jwt 连接
- azure-api-management - 如何在发布者/开发者门户上显示报告
- ios - 为什么 SwiftUI 上下文菜单在预览中显示所有行视图?
- javascript - 在 Microsoft Edge 中使用画布上下文调整图像的亮度
- python - 请有人就代码格式提出建议
- telegram - 电报机器人。如何在用户文本输入中设置命令 /cmd?