Как нажать кнопку CSV на веб-сайте и загрузить данные в python - PullRequest
1 голос
/ 27 апреля 2020

Я пытаюсь загрузить данные CSV и JSON со следующего веб-сайта: https://worldpopulationreview.com/countries/countries-by-gdp/#worldCountries

Как смоделировать нажатие 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')

Ответы [ 2 ]

1 голос
/ 27 апреля 2020

Вам необходимо скачать pip install selenium Если вы используете Chrome, тогда скачайте драйвер Chrome здесь - Chrome драйвер . И затем, найдя xpath кнопки / ссылки, я использовал элемент inspect, чтобы найти 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

1 голос
/ 27 апреля 2020

Вы можете использовать селен, чтобы смоделировать нажатие на кнопку загрузки CSV https://selenium-python.readthedocs.io/getting-started.html#example - объяснено

...