【发布时间】:2021-06-10 01:10:36
【问题描述】:
我是使用 Selenium 抓取的新手,对于如何提取可方便地直接在 div 中使用的 JSON 感到困惑。 div 甚至包含一个名为data-json的标签
我目前的基本代码是:
#this works perfectly
from selenium import webdriver
DRIVER_PATH = '/my/path/to/driver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
driver.get('https://www.genecards.org/cgi-bin/carddisp.pl?gene=IL1R1')
#this errors out
element = driver.find_element_by_data_type('Biological')
#this also errors out
driver.execute_script('return JSON.stringify(window.dataJSON)
我正在寻找的 JSON 是一个 div 中的一个 div,其中最外层的 div 有一个标签 data-type='Biological',而最里面的 div 包含 JSON 数据本身:
请注意,此网页包含一些其他嵌入的 JSON,因此指定父 div 中的 data-type 很重要。任何有关如何将此 JSON 提取为字符串变量的指导都将不胜感激!
【问题讨论】:
标签: python json selenium web-scraping