【问题标题】:How to extract specified numbers from a HTML source and merge them?如何从 HTML 源中提取指定的数字并合并它们?
【发布时间】: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


【解决方案1】:

您在BeautifulSoup(driver.page_source, "lxml") 中忘记了.page_source


至于数字,如果您使用 BeautifulSoup,则使用其所有功能 - 使用 find_all() 查找所有 &lt;img&gt; 并获取属性 src。然后你可以从src(这是字符串)作为string[-5]获取char

如果有更多图像,那么您可以在find_all() 中使用其他属性 - 即。 width="35"

您应该阅读BeautifulSoup 的文档 - 它有很多有用的功能。


html = '''<img width="35" height="55" src="images/capchs/6.png">
<img width="35" height="55" src="images/capchs/3.png">
<img width="35" height="55" src="images/capchs/1.png">'''

from bs4 import BeautifulSoup as BS

soup = BS(html, 'lxml')

all_items = soup.find_all('img')
#all_items = soup.find_all('img', width='35')
for item in all_items:
    print('char:', item['src'][-5])

number = [item['src'][-5] for item in all_items]
number = "".join(number)

print('number:', number)

结果

char: 6
char: 3
char: 1
number: 631

即使没有BeautifulSoup,你也可以这样做,因为Selenium 有很多功能find_elements_by_...(即find_elements_by_xpath()),它也有get_attribute()

阅读 Selenium 文档:Locating Elementsget_attribute

未测试

all_items = driver.find_elements_by_tag_name('img')
for item in all_items:
    print('char:', item.get_attribute('src')[-5])

number = [item.get_attribute('src')[-5] for item in all_items]
number = "".join(number)

print('number:', number)

【讨论】:

  • 很抱歉,我发现这个写得很好的代码存在一些问题。我使用了它,并且返回的 url 比预期的多。它返回了几个字母。我通过传递删除了它们他们通过代码来测试它们是否是数字。我现在唯一剩下的问题是每次刷新页面以获取新数字并单击按钮。我正在考虑为前一个问题循环代码。谢谢你的快速和有用的答案。
  • 当您没有显示有问题的真实网址时会发生这种情况 - 我们看不到真实的 HTML,我们无法创建解决所有问题的代码。正如我在回答中所说 - 也许您可以使用其他属性来仅查找带有数字的图像。要刷新页面,您可以再次使用 driver.get(your_url) 或发送 JavaScript 代码,如 driver.execute_script('document.location=your_url')
  • 不用担心,因为我已经找到了解决方案。在您的帮助下,我进步了很多,但我仍然有一些障碍。我可以将其描述为生成的数字是正确的,但在下一次“调查”我的程序会记下之前制作的数字。我添加了一些睡眠时间但无济于事。除非你能再次帮助我,否则我可以发新帖子。
  • 在新页面上创建新问题 - 您将有更多空间用于描述和代码。新人会看到它,所以也许其他人会帮助你。
猜你喜欢
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 2018-07-30
相关资源
最近更新 更多