使用 scrapy 和 python 从 tsetmc.com 网页抓取网页

webscraping from tsetmc.com webpage using scrapy and python

我想抓取这个网页: http://www.tsetmc.com/loader.aspx?ParTree=151311&i=42354736493447489

这是我的代码:scrapy shell "http://www.tsetmc.com/loader.aspx?ParTree=151311&i=42354736493447489" 并且我想抓取下图中显示的这个价格(价格和相关的chrome inspect如图所示): click to show image #1

然后我写了这段代码response.xpath('//*[@id="dbp]'),但是输出是:[ ] 。 click to show image #2

我有点困惑。因为我想从这个网站 select 每个数字,我得到这个错误。

如果有人能帮助我,我会很高兴。 :)

使用selenium提取javascript动态加载的数据,因为javascript在scrapy中不能运行。

from selenium import webdriver
from bs4 import BeautifulSoup
driver = webdriver.Chrome()

driver.get('http://www.tsetmc.com/loader.aspx?ParTree=151311&i=42354736493447489')
time.sleep(5) # delay 5 sec
page_source = driver.page_source

soup = BeautifulSoup(page_source,'html.parser')
# print(soup.prettify())
prices = soup.find('div', {'class': 'box6 h80'}).find('table')

for td in prices.find_all('tr')[1]:
    print(td.getText()) # all td text garbed.

driver.quit()