python - Web 动态抓取内容包装器数据
问题描述
这里,https://forecast.vassarlabs.com/,我要提取降雨表数据;为了检查表,您必须单击右上角的菜单图标。我想提取这个数据表。
我的代码:
from urllib.request import urlopen
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import os
driver = webdriver.Chrome("C:/Users/DELL/Desktop/chromedriver.exe")
driver.get('https://forecast.vassarlabs.com/')
res = driver.execute_script("return document.documentElement.outerHTML")
driver.quit()
soup = BeautifulSoup(res, 'html.parser')
table = soup.find("div",class_='content-wrap')
ta=table.find('table',class_="table table-bordered table-striped responsive no-m ng-scope")
print(ta)
解决方案
您也可以避免使用 selenium 并直接从 API 中获取。您需要做的唯一部分是解析 json 响应:
import requests
import pandas as pd
url = 'https://forecast.vassarlabs.com/api/commanddashboard/getdashboarddata/RAINFALL/24%20Hrs/District'
jsonData = requests.get(url).json()
jsonData = jsonData['Andhra Pradesh']
results = pd.DataFrame()
for k,v in jsonData.items():
row = pd.DataFrame(v['rainfallDataMap']).head(1)
row['District'] = k
results = results.append(row, sort=False).reset_index(drop=True)
输出:
print (results.to_string())
05:30-11:30 11:30-17:30 17:30-23:30 23:30-05:30 24 Hrs District
0 0.0 0.44 0.00 0.0 0.44 East Godavari
1 0.0 0.00 0.00 0.0 0.00 Anantapur
2 0.0 0.00 0.00 0.0 0.00 Kadapa
3 0.0 0.00 0.00 0.0 0.00 Nellore
4 0.0 0.07 0.00 0.0 0.07 West Godavari
5 0.0 0.00 0.00 0.0 0.00 Srikakulam
6 0.0 0.00 0.00 0.0 0.00 Kurnool
7 0.0 0.00 0.00 0.0 0.00 Chittoor
8 0.0 0.00 0.00 0.0 0.00 Krishna
9 0.0 1.05 0.00 0.0 1.05 Visakhapatnam
10 0.0 0.00 0.00 0.0 0.00 Prakasam
11 0.0 0.12 0.00 0.0 0.12 Total
12 0.0 0.21 0.01 0.0 0.22 Vizianagaram
13 0.0 0.00 0.00 0.0 0.00 Guntur
推荐阅读
- c# - 使用 Microsoft Graph SDK 获取具有特定电子邮件域的所有用户
- css - 如何通过使用 css 选择器从父 div 访问子 div 来为子 div 应用不显示
- oracle-apex - Oracle Apex 交互式网格页脚显示不正确
- c# - VS 自动生成的类错误:类型已经包含定义
- css - 使用节点从用户定义的值更改样式表
- c++ - C++:使用 std::string 的二进制表达式的无效操作数
- applet - Mifare 1k 卡是否支持 Java 卡?
- python - 使用 Pygame 进行自上而下的坦克物理
- clojurescript - CLJS:将 JS 属性设置为对其调用方法的结果
- conda - conda:安装本地包 tarball