简体   繁体   English

Python 请求输出与预期输出不同

[英]Python requests output is different to expected output

I am trying to scrape the Generation table off the following site .我正在尝试从以下站点中删除 Generation 表。

I have the following code:我有以下代码:

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)

However this code cannot find any table in the scraped page even though it is clearly there when looking through inspect.但是,此代码在抓取的页面中找不到任何表格,即使在查看检查时它显然存在。 检查 Could this be an issue with site not scraping correctly?这可能是网站无法正确抓取的问题吗?

Thanks谢谢

This page uses JavaScript to load data and to create table.此页面使用JavaScript加载数据和创建表格。

Using DevTools in Firefox/Chrome I found it load it from在 Firefox/Chrome 中使用DevTools我发现它从

https://ausrealtimefueltype.global-roam.com/api/SeriesSnapshot?time= https://ausrealtimefueltype.global-roam.com/api/SeriesSnapshot?time=

as JSON data作为 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('---')

Result结果

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM