使用 BeautifulSoup Python 单击按钮后获取价值 [英] Getting value after button click with BeautifulSoup Python

查看:33
本文介绍了使用 BeautifulSoup Python 单击按钮后获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取点击按钮后网站给出的值.

I'm trying to get a value that is given by the website after a click on a button.

这里是网站:https://www.4devs.com.br/gerador_de_cpf

可以看到有一个叫Gerar CPF"的按钮,这个按钮提供了一个点击后出现的数字.

You can see that there is a button called "Gerar CPF", this button provides a number that appears after the click.

我当前的脚本打开浏览器并获取值,但我在单击之前从页面获取值,因此该值为空.我想知道是否可以在点击按钮后获取值.

My current script opens the browser and get the value, but I'm getting the value from the page before the click, so the value is empty. I would like to know if it is possible to get the value after the click on the button.

from selenium import webdriver
from bs4 import BeautifulSoup
from requests import get

url = "https://www.4devs.com.br/gerador_de_cpf"

def open_browser():
    driver = webdriver.Chrome("/home/felipe/Downloads/chromedriver")
    driver.get(url)
    driver.find_element_by_id('bt_gerar_cpf').click()

def get_cpf():
    response = get(url)

    page_with_cpf = BeautifulSoup(response.text, 'html.parser')

    cpf = page_with_cpf.find("div", {"id": "texto_cpf"}).text

    print("The value is: " + cpf)


open_browser()
get_cpf()

推荐答案

open_browserget_cpf 完全没有关系...

open_browser and get_cpf are absolutely not related to each other...

实际上你根本不需要get_cpf.点击按钮后等待文本:

Actually you don't need get_cpf at all. Just wait for text after clicking the button:

from selenium.webdriver.support.ui import WebDriverWait as wait

def open_browser():
    driver = webdriver.Chrome("/home/felipe/Downloads/chromedriver")
    driver.get(url)
    driver.find_element_by_id('bt_gerar_cpf').click()
    text_field = driver.find_element_by_id('texto_cpf')
    text = wait(driver, 10).until(lambda driver: not text_field.text == 'Gerando...' and text_field.text)
    return text

print(open_browser())

更新

与请求相同:

import requests

url = 'https://www.4devs.com.br/ferramentas_online.php'
data = {'acao': 'gerar_cpf', 'pontuacao': 'S'}
response = requests.post(url, data=data)
print(response.text)

这篇关于使用 BeautifulSoup Python 单击按钮后获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆