python - Python 请求输出与预期输出不同
问题描述
我正在尝试从以下站点中删除 Generation 表。
我有以下代码:
import requests
from bs4 import BeautifulSoup
source = requests.get('http://reneweconomy.com.au/nem-watch/', headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(source.text, 'html.parser')
table = soup.table["database table"].strip()
print(table)
但是,此代码无法在抓取的页面中找到任何表格,即使在查看检查时它明显存在。 这可能是网站无法正确抓取的问题吗?
谢谢
解决方案
此页面用于JavaScript
加载数据和创建表。
在 Firefox/Chrome 中使用DevTools
我发现它是从
https://ausrealtimefueltype.global-roam.com/api/SeriesSnapshot?time=
作为 JSON 数据
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
url = 'https://ausrealtimefueltype.global-roam.com/api/SeriesSnapshot?time='
r = requests.get(url, headers=headers)
data = r.json()
for item in data['seriesCollection']:
#for key, value in item.items():
# print(key, value)
print('region:', item['metadata']['region']['name'])
print('fuel type:', item['metadata']['fuelType']['name'])
print('value:', item['value'])
print('---')
结果
region: Queensland
fuel type: Black Coal
value: 5536.51307
---
region: Queensland
fuel type: Gas
value: 560.24621
---
region: Queensland
fuel type: Liquid Fuel
value: 0.0
---
region: Queensland
fuel type: Other
value: 23.9
---
region: Queensland
fuel type: Hydro
value: 18.415
---
# etc.
推荐阅读
- html - 如何使用 ngFor 和 table [Angular] 显示矩阵等数组元素
- react-native - 导航到与反应导航混淆的屏幕
- javascript - 在 PHP 中使用 JavaScript 函数结果
- excel - ActiveX 组件无法创建对象,使用 .dll/.tlb 时
- javascript - 如何更改类指定的 h2 标记中的硬编码文本?
- android - 缺少类:com.google.android.aidl.BaseProxy
- python - 如何在使用 pandas 读取 excel 文件时克服 u200d unicode
- javascript - ASP.Net Core 3 将 JSON 数组发布到控制器
- ios - 如何在 iOS 中检测已配对的蓝牙设备唤醒并连接?
- python - 在对象列表中选择具有最大属性的对象