如何在以下HTML代码中使用BeautifulSoup获取文本:<span id =“ pass_0” class =“ text-success”> c#</span>

时间:2019-08-16 14:14:25

标签: python-3.x selenium beautifulsoup

我正在通过一个https://hashkiller.co.uk/Cracker网站通过seleniumbeautifulsoup破解一些哈希,

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import requests
import time

target = requests.get("https://hashkiller.co.uk/Cracker")
soup = BeautifulSoup(target.content, 'html.parser')
driver = webdriver.Chrome(executable_path=r"D:\Download\chromedriver.exe")
#driver.set_window_position(-10000,0)
#240aa2cec4b29c56f3bee520a8dcee7e
driver.get("https://hashkiller.co.uk/Cracker")
hash = input("Hash: ")
hash_box = driver.find_element_by_id("txtHashList").send_keys(hash)
hash_submit = driver.find_element_by_id("btnCrack").click()
time.sleep(5)
hash_table = soup.find('span', {'class': 'text-success'})
a = hash_table.text
print(hash_table)

我希望输出为c# [图片:https://imgur.com/kEegEgY] HTML代码:[html <span id="pass_0" class="text-success">c#</span>]

,但返回:html<span class="text-success">$pass</span> 而不是$pass应该有c#

1 个答案:

答案 0 :(得分:1)

您实际上不是在解析呈现的html。您正在解析requests的html响应。

第二,您想获取第二个元素,因为第一个元素是$pass。另外,将hash更改为其他变量,因为它是python中的函数:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import requests
import time




#target = requests.get("https://hashkiller.co.uk/Cracker")
#soup = BeautifulSoup(target.content, 'html.parser')
driver = webdriver.Chrome("C:/chromedriver.exe")
#driver.set_window_position(-10000,0)
#240aa2cec4b29c56f3bee520a8dcee7e
driver.get("https://hashkiller.co.uk/Cracker")

hash_input = input("Hash: ")

hash_box = driver.find_element_by_id("txtHashList").send_keys(hash_input)

hash_submit = driver.find_element_by_id("btnCrack").click()
time.sleep(5)

soup = BeautifulSoup(driver.page_source, 'html.parser')

hash_table = soup.find_all('span', {'class': 'text-success'})
a = hash_table[1].text
print(hash_table)
print(a)


driver.close()

输出:

[<span class="text-success">$pass</span>, <span class="text-success" id="pass_0">c#</span>]
c#