scrapy 如何使用Python从Highcharts中抓取数据

f4t66c6m  于 2022-11-09  发布在  Python
关注(0)|答案(2)|浏览(179)

我尝试从https://www.transfermarkt.com/neymar/marktwertverlauf/spieler/68290的图表中抓取数据。我尝试使用框中数据的相应xpath访问数据,但似乎不起作用。
我尝试使用Scrapy:

date = response.xpath('//*[@id="highcharts-0"]/div/span/b[1]').get()
market_value =  response.xpath('//*[@id="highcharts-0"]/div/span/b[1]').get()
club = response.xpath('//*[@id="highcharts-0"]/div/span/b[3]').get()
age = response.xpath('//*[@id="highcharts-0"]/div/span/b[4]').get()

我如何使用Scrapy或Selenium从图表中抓取所有数据?

bwntbbo3

bwntbbo31#

在使用HTML主体上的内联JS之后,该数据将在客户端(浏览器)上呈现。
如果要使用Scrapy,则需要正则表达式
例如(未检测)

import re
import json

body = response.body()
data = re.findall(r"(?<=\'series\'\:).*?}}]}]", body)

if not data:
   return None

data = json.loads(data[0])
kcugc4gi

kcugc4gi2#

import time
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver 
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")

driver = webdriver.Chrome(ChromeDriverManager().install(), options = chrome_options)
driver.get(url)
time.sleep(5)

temp = driver.execute_script('return window.Highcharts.charts[0]'
                             '.series[0].options.data')
data = [item for item in temp]
print(data)

相关问题