【问题标题】:Extract Data From SVG从 SVG 中提取数据
【发布时间】:2020-01-14 21:04:45
【问题描述】:

我将以下代码保存到本地 html 文件中

<object id="PriceAdvisorFrame" type="image/svg+xml" data="https://www.kbb.com/Api/3.9.448.0/71071/vehicle/upa/PriceAdvisor/meter.svg?action=Get&amp;intent=buy-used&amp;pricetype=Private Party&amp;zipcode=99517&amp;vehicleid=439604&amp;hideMonthlyPayment=True&amp;condition=verygood&amp;mileage=11795" style="width: 100%;"></object>

当它在 chrome 浏览器中执行时,我试图从 html 中提取成本。我试图解析的 Html 代码如下所示。但是,当使用 selenium 请求文件时,此代码不会出现。

<text xmlns="http://www.w3.org/2000/svg" text-anchor="middle" font-size="14" font-weight="700" fill="#333333" y="-8">$27,938</text>
<text xmlns="http://www.w3.org/2000/svg" text-anchor="middle" font-size="14" font-weight="400" fill="#333333" y="-26">Private Party Value</text>
<text xmlns="http://www.w3.org/2000/svg" text-anchor="middle" font-size="20" font-weight="700" fill="#ffffff" y="-48">$26,995 - $28,888</text>
<text xmlns="http://www.w3.org/2000/svg" text-anchor="middle" font-size="14" font-weight="400" fill="#ffffff" y="-68.8">Private Party Range</text>

到目前为止,这是我的代码:

options = webdriver.ChromeOptions()
options.add_argument('headless')
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36'    
options.add_argument('user-agent={0}'.format(user_agent))
driver = webdriver.Chrome(chrome_options=options)

driver.get('file:///F:/Onedrive/Python/KBB/test.html')
print(driver.find_element_by_css_selector('text').text)

关于如何进行这项工作的任何想法?

【问题讨论】:

  • 不能直接从 url https://www.kbb.com/... 获取数据吗?也许你不需要Seleniumrequests 它会工作得更快。但请记住使用&amp;amp; 而不是&amp;amp;

标签: python html selenium beautifulsoup


【解决方案1】:

你说的'text'不是css_selector,是tag_name。您可以使用.find_elements_* 收集所有元素,然后将它们提取出来。

driver.get('file:///F:/Onedrive/Python/KBB/test.html')

elements = driver.find_elements_by_tag_name('text')
for element in elements:
    text = element.text
    if "$" in text:
        print(text)

【讨论】:

  • 我不知道您是否创建了一个 html 文件并自己测试了代码,但问题是文本标签仅在我从浏览器检查 html 代码时出现。 selenium 驱动程序看到的 HTML 代码只是我在第一个代码块中发布的 HTML 代码。如何让驱动程序执行 html 对象以允许它查看位于 svg 中的数据?
【解决方案2】:

加载到浏览器中的 html 在driver.page_source 中没有您想要的信息,因此您无法以这种方式选择。浏览器本身基于data 属性发出GET 请求,并呈现新内容——但是,该文件并未更新。您可以将.get 发送到data 源或使用requests

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(r'path\chromedriver.exe')
driver.get(r'C:\Users\User\Desktop\test.html')
print(driver.page_source)
driver.get(driver.find_element_by_css_selector('[data]').get_attribute('data'))
elem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR , 'text')))
if elem is not None:
    print(elem.text)

【讨论】:

    【解决方案3】:

    要访问 SVG 元素,您需要使用以下 xpath。

    //*[name()='text']

    //*[local-name()='text']

    试试下面的代码。

    elements=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//*[name()='text']")))
    for ele in elements:
      print(ele.text)
    

    要执行上述代码,您需要导入以下代码。

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-07
      • 2013-08-09
      • 2010-12-19
      • 2015-10-21
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      相关资源
      最近更新 更多