使用BeautifulSoup在python中刮xPath

我正在用python做一个webscraper。我想从网页中获取xPath的代码行。 xPath是/html/body/div[2]/div[2]/svg/text[1]/tspan[4]

我可以去div[2],但不能再走了。我的代码是:

one_a_tag = soup.findAll('div')[2]

如何到达xPath的目的地?

aishine 回答:使用BeautifulSoup在python中刮xPath

如果要使用xpath查找项目,则必须使用lxml方法。

from lxml import html
import requests

URL = "Your page url here"
page = requests.get(URL)
tree = html.fromstring(page.content)

myItem = tree.xpath('/html/body/div[2]/div[2]/svg/text[1]/tspan[4]')
print(myItem)
print(myItem.text)
本文链接:https://www.f2er.com/3084276.html

大家都在问