【发布时间】:2020-08-22 08:10:49
【问题描述】:
我对 Web Scraping 非常陌生,我非常强调新。 我需要从网站上的表格中抓取数据。该表每天都在变化(股票价格)。到目前为止,我的代码只提取一天的数据,但我需要一次提取多天的数据。该网页有一个日历,您可以选择一天并显示其历史记录。 我正在使用硒。 这是我的部分代码,向您展示我在做什么`
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_path = "C:\Program Files (x86)\chromedriver.exe"
chrome_options = Options()
chrome_options.add_argument("headless")
driver = webdriver.Chrome(chrome_path , options = chrome_options , keep_alive = False)
driver.get("http://www.casablanca-bourse.com/bourseweb/indice-ponderation.aspx?Cat=22&IdLink=298")
codelist = []
instrumentList = []
NbreList = []
CoursList = []
FacteurList = []
FacteurPlafList = []
Capitalist = []
poidList = []
for i in range(4,77):
codepath = f"""//*[@id="Ponderation1_UpdatePanel1"]/table/tbody/tr[5]/td/table/tbody/tr[4]/td[2]"""
code = driver.find_element_by_xpath(codepath)
codelist.append(code.text)
【问题讨论】:
-
我认为您的上述代码即使在一个日期内也无法抓取数据。根据您上面的列表 -codelist 将不包含 Code Isin 列 73 次(因为您正在循环 73 次)的第一个值,即 MA0000011488 默认日期 18/08/20
-
这只是代码的一部分。该列表还有其他项目,并且运行良好。我压缩了列表中的元素
-
可能是我忘记把{i}加到tr
标签: python selenium web-scraping