Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用Selenium(无头)检索Javascript图表后面的数据/数组_Javascript_Python_Selenium_Web Scraping_Raphael - Fatal编程技术网

无法使用Selenium(无头)检索Javascript图表后面的数据/数组

无法使用Selenium(无头)检索Javascript图表后面的数据/数组,javascript,python,selenium,web-scraping,raphael,Javascript,Python,Selenium,Web Scraping,Raphael,我试图在这个网站上浏览历史“市场价值发展”图表: 在了解到它是javascript之后,我开始学习使用webdrivers(Selenium)、headless浏览器和Chrome/Chrome创建webscraping JS。查看页面后,我发现我可能要查找的ID是ID\u0='yw0',它似乎包含了图表: 鉴于此,以下是我的代码: import selenium as se from selenium import webdriver options = se.webdriver.Chr

我试图在这个网站上浏览历史“市场价值发展”图表:

在了解到它是javascript之后,我开始学习使用webdrivers(Selenium)、headless浏览器和Chrome/Chrome创建webscraping JS。查看页面后,我发现我可能要查找的ID是
ID\u0='yw0'
,它似乎包含了图表:

鉴于此,以下是我的代码:

import selenium as se
from selenium import webdriver

options = se.webdriver.ChromeOptions()
options.add_argument('headless')

driver = se.webdriver.Chrome(executable_path='/Applications/Utilities/chromedriver', chrome_options=options)
driver.get('https://www.transfermarkt.com/neymar/marktwertverlauf/spieler/68290')
element = driver.find_element_by_id(id_='yw0')

print(element)
当我运行它时,它会输出以下内容:

<selenium.webdriver.remote.webelement.WebElement (session="bd8e42834fcdd92383ce2ed13c7943c0", element="8df128aa-d242-40a0-9306-f523136bfe57")>
我得到:

Current Market Value : 180,00 Mill. €
2010
2012
2014
2016
2018
50,0
100,0
150,0
200,0
这不是数据,而是图表间隔的x和y值

我尝试了图表的不同id标记,以查看是否只是识别了错误的容器(例如highcharts-0)。但我无法找到图表的实际数据值

奇怪的是,在我运行代码后,图表会发生一些变化。图表“变宽”并超出图表的指定区域。看起来是这样的:


我想知道我能做些什么,需要在代码中做些什么来刮取图表上显示的数据点。

您可以从javascript中正则化它,并进行一些字符串操作。您可以从下面的列表中获得词典。不需要硒

import requests, re, ast

r = requests.get('https://www.transfermarkt.com/neymar/marktwertverlauf/spieler/68290', headers = {'User-Agent':'Mozilla/5.0'})
p = re.compile(r"'data':(.*)}\],")
s = p.findall(r.text)[0]
s = s.encode().decode('unicode_escape')
data = ast.literal_eval(s)

查看第一项:


Regex:


tl;博士

使用browser on load时,jQuery从
脚本
标记中提取图表信息,从而生成您所看到的内容。正则表达式提取相同的信息,即图表的相关系列信息,jQuery从中获取系列信息


硒:

当然,这方面还有改进的余地,但它表明了一般原则。当您将鼠标悬停在图表上的每个数据点上时,将从脚本标记中检索这些值以更新工具提示。检索到的值与图表点的x、y相关联。因此,您无法从查看工具提示信息的位置进行读取。相反,您可以单击每个数据点并从tooltip元素中获取更新的信息

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from bs4 import BeautifulSoup as bs
from selenium.webdriver.chrome.options import Options
import time

options = Options()
options.add_argument("--start-maximized")

url = 'https://www.transfermarkt.com/neymar/marktwertverlauf/spieler/68290'
d = webdriver.Chrome(options = options)
d.get(url)
WebDriverWait(d, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".as-oil__btn-optin"))).click()
markers = d.find_elements_by_css_selector('.highcharts-markers image')
time.sleep(1)
for marker in markers:
    ActionChains(d).click_and_hold(marker).perform()
    text = d.find_element_by_css_selector('div.highcharts-tooltip').text
    while True:
        if len(text) == 0:
            ActionChains(d).click_and_hold(marker).perform()
        else:
            break
    print(text)

这是你想要的吗?是的!非常感谢。我有3次跟进。1.在决定使用正则表达式之前,您如何知道/找到源代码中的javascript数据?(以免使用硒)2。由于数据已位于页面上,因此使用selenium是否不起作用?三,。索引引用(
[0]
)指的是什么?1。经验动态加载的内容可以来自多个地方,但必须来自某个地方。其中一个地方可以是DOM中稍后从中提取脚本的其他地方。使用selenium会起作用,然后内容就会出现在您在浏览器中访问网页时看到的地方——浏览器将允许运行js脚本并更新DOM。findall返回正则表达式模式的所有匹配项的列表。我想要第一场比赛。您也可以使用search。我编写正则表达式只返回一个匹配项,但当它通过findall应用时,将返回一个列表(在本例中长度为1)。我使用0从列表中提取项目,因此我有一个字符串,而不是一个列表,用于编码/解码并传递给AST关于您尝试使用selenium失败的原因-今天稍后我将查看。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from bs4 import BeautifulSoup as bs
from selenium.webdriver.chrome.options import Options
import time

options = Options()
options.add_argument("--start-maximized")

url = 'https://www.transfermarkt.com/neymar/marktwertverlauf/spieler/68290'
d = webdriver.Chrome(options = options)
d.get(url)
WebDriverWait(d, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".as-oil__btn-optin"))).click()
markers = d.find_elements_by_css_selector('.highcharts-markers image')
time.sleep(1)
for marker in markers:
    ActionChains(d).click_and_hold(marker).perform()
    text = d.find_element_by_css_selector('div.highcharts-tooltip').text
    while True:
        if len(text) == 0:
            ActionChains(d).click_and_hold(marker).perform()
        else:
            break
    print(text)