python-3.x - 使用硒进行网页抓取时如何获取文本值?
问题描述
我正在抓取一个页面。<span class="product_content_brand"> NikeLab </span>
我在 python3 上得到了带有硒的元素。
from selenium import webdriver
browser= webdriver.Chrome("/home/desarrollo10/Downloads/
chromedriver_linux64/chromedriver")
browser.get("https://theurge.com.au/")
C=browser.find_element_by_tag_name("a").click()
time.sleep(0.5)
D=browser.find_element_by_class_name("tag-filters_clearall").click()
S=browser.find_elements_by_class_name("product_content")
for s in S:
print(s.text)
我想从“product_content”类的元素中获取文本,我得到:
WebDriverException:消息:chrome 无法访问(会话信息:chrome=71.0.3578.98)(驱动程序信息:chromedriver=2.44.609551(5d576e9a44fe4c5b6a07e568f1ebc753f1214634),平台=Linux 4.15.0-43-通用 x86_64)
解决方案
尝试在此处找到有关在启动 Chrome 时添加几个参数(no-sandbox、disable-setuid-sandbox)的可能解决方案:
chrome_options = Options()
#argument to switch off suid sandBox and no sandBox in Chrome
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-setuid-sandbox")
browser= webdriver.Chrome("/home/desarrollo10/Downloads/chromedriver_linux64/chromedriver", chrome_options=chrome_options)
然后还有:
我没有看到class = "tag-filters_clearall"
,而是class = "tag-filters_clear-all"
所以我认为你的意思是:
D=browser.find_element_by_class_name("tag-filters_clear-all").click()
不是:
D=browser.find_element_by_class_name("tag-filters_clearall").click()
推荐阅读
- css - 在 NativeScript 上支持表格和电话的单独 CSS
- javascript - 使用输入键作为选项卡的困难
- php - 无法使用数组更新帖子元
- http - netflix 指挥 - http 类型任务 - 不应该从工作流开始 - 实现重试
- monitoring - 每小时或每天向 Prometheus 报告一个指标
- java - java - 为什么java socket在遇到“连接重置”异常时没有将其“关闭”状态设置为真?
- mesh - 在 GMSH 中为多个实体创建单个网格
- python - 使用 python 使用 pandas 读取文本文件
- android - 华为 mya-L22 将应用程序置于后台并关闭屏幕时 GPS 位置更新停止更新
- asp.net-identity - Asp.net core 身份批量插入用户