簡體   English   中英

我如何使用帶有 xpath 的 pythonl 美麗湯從 tspan 標簽中獲取文本

[英]how can i get the text from tspan tag by using pythonl beatiful soup with xpath

from bs4 import BeautifulSoup
from lxml import etree
import requests
from lxml import html

url="https://www.findchips.com/detail/TS63Y502KR10/2516-Vishay%20Intertechnologies?quantity=1"
Dict_Headers = ({'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'})

webPage = requests.get(url,Dict_Headers)

Scraping = BeautifulSoup(webPage.content, "html.parser") 

documentObjectModel = etree.HTML(str(Scraping)) 


Distri_Name=documentObjectModel.xpath('//span[@class="distri-name"]')
Inventory_Number=documentObjectModel.xpath('//span[@class="inventory-number"]')

Infor_Number=documentObjectModel.xpath('//tspan[@class="point-info-number large"]')
print(len(Infor_Number))

print(len(Infor_Number)) # 返回 0 表示我沒有從發現中返回任何內容。 #我需要獲取此 tspan 上的文本,即 $3.7200 #please help??

*problem solved by myself today! the following is the code*

from selenium import webdriver
import time
import requests
from bs4 import BeautifulSoup
from selenium.webdriver.common import by
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

import re


from selenium.webdriver.chrome.options import Options


s=Service(ChromeDriverManager().install())
#options = Options()
#options.add_argument('headless')
# then pass options to the driver
#driver=webdriver.Chrome(executable_path="C:\\Users\\Purchasing\\pythontest\chromedriver_win32\\chromedriver.exe")
#driver = webdriver.Chrome(service=s, options=options) 

driver = webdriver.Chrome(service=s)
driver.maximize_window()
driver.get('https://www.findchips.com/detail/TS63Y502KR10/2516-Vishay%20Intertechnologies?quantity=10')#('https://codeforces.com/profile/akash77')
elements = driver.find_elements(By.XPATH, '//*[@class="j-analytics-pricing-graph pricing-graph"]')
print(len(elements))
svg = [WebElement.get_attribute('innerHTML') for WebElement in elements]
Great_Price_less=str(svg).split("point-info-number large")[-1].split("point-info-text large")[-2].split("<")[-3].split(">")[-1]
Great_Price_more=str(svg).split("point-info-number large")[-2].split("point-info-text large")[-2].split("<")[-3].split(">")[-1]

print(Great_Price_more + " " + Great_Price_less)

driver.quit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM