基于日期用python抓取表格 [英] scraping table with python based on dates

查看:49
本文介绍了基于日期用python抓取表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一周前开始,我一直试图从这个网站上刮一张桌子 但问题是默认日期和我选择的日期之间的链接仍然相同.请以任何方式帮助我,谢谢!

since a week ago i have been trying to scrape a table from this site https://www.bi.go.id/id/moneter/informasi-kurs/transaksi-bi/Default.aspx but i dont have an idea what to write,i am very confused. iam trying to scrape table of kurs transaction from 2015-2020(20 nov 2015-20 nov 2020, but the problem is the link between the default date and the date that I chose is still the same.please help me in any way,Thank you before !

import requests
from bs4 import BeautifulSoup
import pandas as pd
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36",
"X-Requested-With":"XMLHttpRequest"
}
url = "https://www.bi.go.id/id/moneter/informasi-kurs/transaksi-bi/Default.aspx"
import requests
from lxml import html
response = requests.get(url)
content= response.content
print(content)

推荐答案

你需要使用 Selenium.您可以先安装 Selenium,然后再安装 驱动程序.我使用 chrome,然后在安装后记下该路径并将 DRIVER_PATH 设置为该位置

You need to use Selenium. You can install Selenium and then you can install a driver. I use chrome and then once you install it make a note of that path and set your DRIVER_PATH to the location

在下面的代码中,我所做的基本上是请求您发布的链接,然后我输入您可以更改的日期.最后我点击提交按钮.这会在日期范围内生成表格.现在您可以编写后续代码来从表格中抓取信息.

In the code below what i do is basically request the link you posted and then I enter the dates which you can change. Finally i click on the submit button. That generates the table within the date range. Now you can write follow up code to scrape the information from the table.

代码

import requests
from selenium import webdriver

DRIVER_PATH = 'Yourpath/chromedriver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
driver.get('https://www.bi.go.id/id/moneter/informasi-kurs/transaksi-bi/Default.aspx')
start_date = driver.find_element_by_id("ctl00_PlaceHolderMain_biWebKursTransaksiBI_txtFrom")
start_date.send_keys("15-Nov-20")
end_date = driver.find_element_by_id("ctl00_PlaceHolderMain_biWebKursTransaksiBI_txtTo")
end_date.send_keys("20-Nov-20")
submit_button = driver.find_element_by_id("ctl00_PlaceHolderMain_biWebKursTransaksiBI_btnSearch1").click()

这篇关于基于日期用python抓取表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆