如何使用 Selenium 和 BeautifulSoup 从标签中获取文本

基里尔·博罗沃伊

有我需要获取的验证码的文本描述。但是我不能用 BeautifulSoup 得到它。请帮助修复它。

当我运行代码时:“无”是结果。

import os, urllib.request, requests, datetime, time, random, ssl, json, codecs, csv, urllib
from urllib.request import Request, urlopen
from urllib.request import urlretrieve
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup


chromedriver = "chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
os.environ["webdriver.chrome.driver"] = chromedriver
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chrome_options)
driver.set_window_size(1050, 740)
driver.get("https://www.inipec.gov.it/cerca-pec/-/pecs/companies")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='recaptcha-checkbox goog-inline-block recaptcha-checkbox-unchecked rc-anchor-checkbox']/div[@class='recaptcha-checkbox-checkmark']"))).click()
time.sleep(5)
html = driver.page_source()
soup = BeautifulSoup(html, 'html.parser')
title = soup.find('div',{'class':'rc-imageselect-desc-wrapper'})
print (title)
driver.quit()

在此处输入图片说明

尤文克

您正在寻找的内容不同,iframe您需要另一个切换到框架

.....
time.sleep(5)
# go to parent or top frame
driver.switch_to.default_content()
iframe = driver.find_element_by_css_selector('iframe[title="recaptcha challenge"]')
driver.switch_to.frame(iframe)
#title = driver.find_element_by_css_selector('.rc-imageselect-desc-wrapper strong') # cars
title = driver.find_element_by_class_name('rc-imageselect-desc-wrapper') # Select all images with cars.....
print(title.text)

# with BeautifulSoup
#html = driver.page_source()
#soup = BeautifulSoup(html, 'html.parser')
#title = soup.find('div',{'class':'rc-imageselect-desc-wrapper'})
#print (title)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用Selenium和python获取作为标签中符号的文本?

来自分类Dev

如何使用Selenium和python获取作为标签中符号的文本?

来自分类Dev

如何从BeautifulSoup中的span标签获取文本

来自分类Dev

使用BeautifulSoup和Python从item标签获取地址文本

来自分类Dev

使用 Python、Selenium 和 BeautifulSoup 来抓取标签的内容?

来自分类Dev

在 Angular 网站中,使用 Selenium 和 Python 获取 <div> 标签内的确切文本?

来自分类Dev

如何使用Python Selenium和BeautifulSoup从网络上刮掉所有<li>文本?

来自分类Dev

如何从具有带有已知文本的标题标签的Div中获取Table元素-使用Selenium和Java

来自分类Dev

使用Selenium和BeautifulSoup搜寻网站

来自分类Dev

使用BeautifulSoup和Selenium解析HTML内容

来自分类Dev

如何使用Selenium WebDriver中的文本获取标签的索引/位置?

来自分类Dev

网页抓取 - 从使用 BeautifulSoup 和 Python 的类中获取文本?

来自分类Dev

如何使用Selenium和Python在跨度内的标签内获取文本?

来自分类Dev

如何使用beautifulsoup从链接获取文本和URL

来自分类Dev

如何使用beautifulsoup获取包含在包含多个子标签的标签中的文本?

来自分类Dev

如何使用BeautifulSoup和Python获取元素

来自分类Dev

如何使用Python和BeautifulSoup中的类过滤标签?

来自分类Dev

使用 selenium webdriver 从嵌套的 div 标签中获取文本

来自分类Dev

使用BeautifulSoup获取没有标签的文本

来自分类Dev

使用beautifulsoup从br标签获取文本

来自分类Dev

使用BeautifulSoup获取没有标签的文本?

来自分类Dev

使用来自 AJAX 网站的 selenium 和 beautifulsoup 在 python 中抓取图像

来自分类Dev

如何使用Selenium和Python在iframe中获取<p>标记中的文本?

来自分类Dev

如何使用selenium和xpath获取这些段落的文本

来自分类Dev

python - 如何使用beautifulsoup在网页中的某个文本之前获取所有<p>标签?

来自分类Dev

如何使用在 BeautifulSoup 中应用的正则表达式从 <p> 标签获取文本?

来自分类Dev

在使用BeautifulSoup忽略格式标签的同时,如何从html获取文本?

来自分类Dev

如何在Python中使用Beautifulsoup获取嵌套标签的文本?

来自分类Dev

Selenium和BeautifulSoup无法获取所有HTML内容

Related 相关文章

  1. 1

    如何使用Selenium和python获取作为标签中符号的文本?

  2. 2

    如何使用Selenium和python获取作为标签中符号的文本?

  3. 3

    如何从BeautifulSoup中的span标签获取文本

  4. 4

    使用BeautifulSoup和Python从item标签获取地址文本

  5. 5

    使用 Python、Selenium 和 BeautifulSoup 来抓取标签的内容?

  6. 6

    在 Angular 网站中,使用 Selenium 和 Python 获取 <div> 标签内的确切文本?

  7. 7

    如何使用Python Selenium和BeautifulSoup从网络上刮掉所有<li>文本?

  8. 8

    如何从具有带有已知文本的标题标签的Div中获取Table元素-使用Selenium和Java

  9. 9

    使用Selenium和BeautifulSoup搜寻网站

  10. 10

    使用BeautifulSoup和Selenium解析HTML内容

  11. 11

    如何使用Selenium WebDriver中的文本获取标签的索引/位置?

  12. 12

    网页抓取 - 从使用 BeautifulSoup 和 Python 的类中获取文本?

  13. 13

    如何使用Selenium和Python在跨度内的标签内获取文本?

  14. 14

    如何使用beautifulsoup从链接获取文本和URL

  15. 15

    如何使用beautifulsoup获取包含在包含多个子标签的标签中的文本?

  16. 16

    如何使用BeautifulSoup和Python获取元素

  17. 17

    如何使用Python和BeautifulSoup中的类过滤标签?

  18. 18

    使用 selenium webdriver 从嵌套的 div 标签中获取文本

  19. 19

    使用BeautifulSoup获取没有标签的文本

  20. 20

    使用beautifulsoup从br标签获取文本

  21. 21

    使用BeautifulSoup获取没有标签的文本?

  22. 22

    使用来自 AJAX 网站的 selenium 和 beautifulsoup 在 python 中抓取图像

  23. 23

    如何使用Selenium和Python在iframe中获取<p>标记中的文本?

  24. 24

    如何使用selenium和xpath获取这些段落的文本

  25. 25

    python - 如何使用beautifulsoup在网页中的某个文本之前获取所有<p>标签?

  26. 26

    如何使用在 BeautifulSoup 中应用的正则表达式从 <p> 标签获取文本?

  27. 27

    在使用BeautifulSoup忽略格式标签的同时,如何从html获取文本?

  28. 28

    如何在Python中使用Beautifulsoup获取嵌套标签的文本?

  29. 29

    Selenium和BeautifulSoup无法获取所有HTML内容

热门标签

归档