gpt4 book ai didi

python - 无法使用 Selenium Python 以表格形式获取数据

转载 作者:行者123 更新时间:2023-12-04 09:38:37 25 4
gpt4 key购买 nike

我是新来的 刮刮使用 Selenium python 。所以我可以检索一些数据,但我希望它以表格形式显示在网页上:
这是我到目前为止所拥有的:

url='https://definitivehc.maps.arcgis.com/home/item.html?id=1044bb19da8d4dbfb6a96eb1b4ebf629&view=list&showFilters=false#data'

browser = webdriver.Chrome(r"C:\task\chromedriver")
browser.get(url)
time.sleep(25)


rows_in_table = browser.find_elements_by_xpath('//table[@class="dgrid-row-table"]//tr[th or td]')
for element in rows_in_table:
print(element.text.replace('\n', ''))
结果片段:
Hospital NameHospital TypeCityState AbrvZip CodeCounty NameState Name
Phoenix VA Health Care System (AKA Carl T Hayden VA Medical Center)VA HospitalPhoenixAZ85012MaricopaArizona040130401362620000.001
Southern Arizona VA Health Care SystemVA HospitalTucsonAZ85723PimaArizona04019040192952952202.002
VA Central California Health Care SystemVA HospitalFresnoCA93703FresnoCalifornia060190601954542202.003
VA Connecticut Healthcare System - West Haven Campus (AKA West Haven VA Medical Center)VA HospitalWest HavenCT6516New HavenConnecticut09009090092162161102.004
我真的很感激这方面的专家的帮助。谢谢。

最佳答案

这是@Andrej 回答的更新版本,此代码将下载表格,而不是打印,将其保存为 excel 文档。

import json
import requests
import pandas as pd
from pandas.io.json import json_normalize

config_url = 'https://definitivehc.maps.arcgis.com/sharing/rest/portals/self?culture=en-us&f=json'
page_url = 'https://services7.arcgis.com/{_id}/arcgis/rest/services/Definitive_Healthcare_USA_Hospital_Beds/FeatureServer/0/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=OBJECTID%20ASC&resultOffset={offset}&resultRecordCount=50&cacheHint=true&quantizationParameters=%7B%22mode%22%3A%22edit%22%7D'

_id = requests.get(config_url).json()['id']
required=[]
offset = 0
while True:
data = requests.get(page_url.format(_id=_id, offset=offset)).json()

# uncommnet this to print all data:
#pprint(json.dumps(data, indent=4))

for i, f in enumerate(data['features'], offset+1):
required.append(f['attributes'])


if i % 50:
break

offset += 50

df=json_normalize(required)
with pd.ExcelWriter('dataFunction.xlsx', mode='A') as writer:
df.to_excel(writer)

我试过这个并上传了excel表 HERE(LINK TO EXCEL SHEET) !

关于python - 无法使用 Selenium Python 以表格形式获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62433324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com