簡體   English   中英

如何在網站中單擊 CSV 按鈕並下載 python 中的數據

[英]How to click the CSV button in a website and download the data in python

我正在嘗試從以下網站下載 CSV 和 JSON 數據: https://worldpopulationreview.com/countries/countries-by-gdp/

如何模擬點擊 csv 文件?

import pandas as pd
import requests
from lxml import html,etree

url = "https://worldpopulationreview.com/countries/countries-by-gdp/#worldCountries"

# now I am not sure, how to click csv button of actual website
# also I am not sure how it will download the csv file
# to DOWNLOADS as like when I click the page

我可以抓取網頁,但我想學習點擊按鈕

import pandas as pd
import requests

url = "https://worldpopulationreview.com/countries/countries-by-gdp/#worldCountries"

r = requests.get(url)
df = pd.read_html(r.text)[0]
df.to_csv('data.csv')

您需要下載pip install selenium如果使用 Chrome,請在此處下載 Chrome 驅動程序 - Chrome 驅動程序 然后找到按鈕/鏈接的 xpath,我使用檢查元素找到 xpath:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(executable_path='/Users/xxx/Downloads/chromedriver-1')
driver.get('https://worldpopulationreview.com/countries/countries-by-gdp')#put here the adress of your page
btn = driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div[2]/div[2]/div[1]/div/div/div/div[2]/div[1]/a[2]')
btn.click()
df = pd.read_csv('/Users/xxx/Downloads/data.csv')
print(df.head())
driver.close()

   rank         country        imfGDP           unGDP  gdpPerCapita          pop
0     1   United States  2.219812e+13  18624475000000    67063.2695   331002.651
1     2           China  1.546810e+13  11218281029298    10746.7828  1439323.776
2     3           Japan  5.495420e+12   4936211827875    43450.1405   126476.461
3     4         Germany  4.157120e+12   3477796274497    49617.1450    83783.942
4     6  United Kingdom  2.927080e+12   2647898654635    43117.5725    67886.011

查找 xpath 的圖像: Xpath

您可以使用 selenium 來模擬點擊 csv 下載按鈕https://selenium-python.readthedocs.io/getting-started.html##

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM