[英]How to extract text from svg using python selenium
我正在嘗試從鏈接中獲取價格: https://www.kbb.com/cadillac/deville/1996/sedan-4d/
價格顯示在svg標簽內的文本標簽中。
當我在瀏覽器的檢查元素中使用 xpath: .//*[name()='svg']//*[name()='g']//*[name()='text']
時,我'能夠找到標簽。 但是相同的 xpath 在代碼中不起作用。
當前代碼是:
def get_price(url):
driver.get(url)
time.sleep(10)
try:
price_xpaths = driver.find_elements_by_xpath(".//*[name()='svg']//*[name()='g']//*[name()='text']")
except:
print("price not found")
for p in price_tags:
print(p.text)
當我運行上述代碼時,我得到一個空白列表以返回 function find_elements_by_xpath。 我嘗試了其他事情以及切換到默認內容,因為該元素在#document
driver.switch_to_default_content()
但這也沒有奏效。 如果有其他方法可以刮價格,請告訴我。
It is external SVG
and it seems Selenium doesn't have it in DOM so I had to get <object>
which has url to this SVG
file, get this url in data
, download it using requests
and get text using BeautifulSoup
from selenium import webdriver
import time
import requests
from bs4 import BeautifulSoup
url = 'https://www.kbb.com/cadillac/deville/1996/sedan-4d/'
driver = webdriver.Firefox()
driver.get(url)
time.sleep(5)
# doesn't work - always empty list
#price_xpaths = driver.find_elements_by_xpath(".//*[name()='svg']//*[name()='g']//*[name()='text']")
#price_xpaths = driver.find_elements_by_xpath('//svg')
#price_xpaths = driver.find_elements_by_xpath('//svg//g//text')
#price_xpaths = driver.find_elements_by_xpath('//*[@id="PriceAdvisor"]')
#print(price_xpaths) # always empty list
# single element `object`
svg_item = driver.find_element_by_xpath('//object[@id="PriceAdvisorFrame"]')
# doesn't work - always empty string
#print(svg_item.get_attribute('innerHTML'))
# get url to file SVG
svg_url = svg_item.get_attribute('data')
print(svg_url)
# download it and parse
r = requests.get(svg_url)
soup = BeautifulSoup(r.content, 'html.parser')
text_items = soup.find_all('text')
for item in text_items:
print(item.text)
結果:
Fair Market Range
$1,391 - $2,950
Fair Purchase Price
$2,171
Typical
Listing Price
$2,476
順便說一句:其他用戶的信息:我必須使用代理/ VPN和位於US
的 IP 才能看到這個頁面。 對於位置PL
,它顯示
Access Denied.
You don't have permission to access "http://www.kbb.com/cadillac/deville/1996/sedan-4d/" on this server.
有時即使是在US
的位置,它也會給我這個信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.