python - 通过 XPath 转换为来自 Selenium Find Element 的请求
问题描述
我正在使用 Selenium Webdriver 使用“通过 XPath 查找元素”从网站中提取价格,然后将其转换为文本。现在我需要改用 Requests 而不是 Selenium,而且我似乎遇到了多个问题。
有人可以帮我将此代码转换为请求格式吗?
listprice = browser.find_element_by_xpath("//span[@class='title price']")
price = listprice.text
print(price)
我尝试使用 QHarr 下面提到的方法,但出现以下错误:
AttributeError: 'NoneType' object has no attribute 'text'
解决方案
如果您确定请求响应中存在该元素
import requests
from bs4 import BeautifulSoup as bs
r = requests.get(url) # < insert your url. Headers may be required.
soup = bs(r.content, 'html.parser')
price = soup.select_one('span[class="title price"]').text
print(price)
推荐阅读
- reactjs - 如何将数组的初始化包装在它自己的 useMemo() Hook 中?
- python - 没有matplotlib的豆机/高尔顿板球位置直方图
- python - 如何解决以下问题,这是来自codingbat的鹦鹉问题?
- reactjs - React hooks:如何使用 useState() 更新嵌套对象的状态?
- outlook - Outlook 要求 Office 365/Microsoft 登录而不是 Exchange 登录
- flutter - 为什么多次调用文本更改侦听器函数而不更改文本
- c# - .net 核心注册通用接口并使用 getservice 检索它们
- php - fgetcsv 跳过 csv 文件中的空白列并给出偏移错误
- jenkins - Jenkins:将插件 DSL 函数添加到共享库函数时没有这样的 DSL 方法
- django - 如何在没有中间表的情况下处理 Django 中的多对多关系?