【发布时间】:2020-02-19 20:56:21
【问题描述】:
有一个网站提供 3 个数字作为图片,您必须复制并写入指定的框中,然后按继续。我想为我编写代码。我查看了 HTML 源和 png 文件与数字命名相同,所以我只需要提取它们,合并并写下来。
我已经使用 Selenium 制作了一个机器人并在我登录后访问该网站,它会在指定区域填充“123”作为测试,所以我知道如果我以某种方式获得了这些数字如何写下它们。我使用 Beatifulsoup 来把它变成文本,但它给了我一个错误
File "C:\Users\user\Desktop\money.py", line 20
soup = BeautifulSoup(driver)
UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.
The code that caused this warning is on line 20 of the file C:\Users\user\Desktop\money.py. To get rid of this warning, pass the additional argument 'features="lxml"' to the BeautifulSoup constructor.
Traceback (most recent call last):
File "C:\Users\user\Desktop\money.py", line 20, in <module>
soup = BeautifulSoup(driver)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\bs4\__init__.py", line 287, in __init__
elif len(markup) <= 256 and (
TypeError: object of type 'WebDriver' has no len()
如果您有其他方法或可以解决问题,我们将不胜感激。 我还没有弄清楚如何自动按下继续按钮,但如果需要,我会自己按下它。 我的代码甚至还没有完成,所以请随意提出任何建议。
import time
import re
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path = "C:/Users/user/Desktop/Personal/PythonScripts/chromedriver.exe")
driver.get('URL I USED')
time.sleep(20)
driver.refresh()
try :
driver.find_element_by_tag_name('input').send_keys('123')
except :
print('Fail')
soup = BeautifulSoup(driver ,"lxml")
images = list()
try :
re.findall(r'\d+', soup)
images.append(new)
except:
print('Fail')
html_source = driver.page_source
print(html_source)```
【问题讨论】:
-
总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图)放在有问题的(不是评论)中。还有其他有用的信息。
-
您在
BeautifulSoup(driver.page_source, "lxml")中忘记了.page_source -
非常感谢。如果我应该发一个新帖子,我不知道,但是如何从“"。这个数字总是在 1 -9 之间,我需要在一个指定的框中写 3 个。(我在帖子中写了一点)
-
The code that caused this warning is on line 20 of the file C:\Users\user\Desktop\money.py. To get rid of this warning, pass the additional argument 'features="lxml"' to the BeautifulSoup constructor.这行给你提示。始终注意回溯消息。 -
。我已经在代码中有“lxml”,但错误仍然存在,但在我添加了@furas 告诉我的内容后,它们都消失了
标签: python python-3.x selenium beautifulsoup