【问题标题】:Can't find forms inside a html code - Web Scraping with Python and Selenium在 html 代码中找不到表单 - Web Scraping with Python and Selenium
【发布时间】:2017-08-30 20:25:32
【问题描述】:

我正在尝试废弃this website,但需要填写一些表格。

主要目的是填写这5个表格(选择另一个后出现)并通过“顾问”按钮下载数据。

这些表单是用 javascript 编码的,我在页面的 html 代码中找不到它们。当我通过 Google Chrome 检查框架时,我找到了表单 ID,但代码没有找到它们。

我只有一个代码原型。如果不知道该怎么做才能找到这些表格,我就无法前进。

from selenium import webdriver
from bs4 import BeautifulSoup
import time
import os

#Variables

url = 'http://www.anbima.com.br/pt_br/informar/sistema-reune.htm'
path_phantom = 'C:\\Users\\TBMEPYG\\AppData\\Local\\Continuum\\Anaconda3\\Lib\\site-packages\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe'

#Processing

driver = webdriver.PhantomJS(executable_path= path_phantom)

driver.get(url)

data = driver.find_element_by_id('data_ref')

data.send_keys("21/08/2017")

driver.quit()

编辑:

我将代码更新为:

    from selenium import webdriver

    path_phantom = 'C:\\Users\\TBMEPYG\\AppData\\Local\\Continuum\\Anaconda3\\Lib\\site-packages\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe'
    driver = webdriver.PhantomJS(executable_path= path_phantom)
    driver.get('http://www.anbima.com.br/reune/reune.asp')


    driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@class="full"]'))
    data = driver.find_element_by_name('Dt_Ref')
    data.clear()
    data.send_keys('21/08/

我得到了这个错误:

CD: C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3
Current directory: C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3
python "C:\Users\TBMEPYG\Desktop\vamo.py"
Process started >>>
Traceback (most recent call last):
  File "C:\Users\TBMEPYG\Desktop\vamo.py", line 8, in <module>
    data = driver.find_element_by_name('Dt_Ref')
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 426, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 832, in find_element
    'value': value})['value']
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with name 'Dt_Ref'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"89","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:62040","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"name\", \"value\": \"Dt_Ref\", \"sessionId\": \"bdd3fc70-8dd0-11e7-aeb1-85b8cfbe0d1c\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/bdd3fc70-8dd0-11e7-aeb1-85b8cfbe0d1c/element"}}
Screenshot: available via screen

编辑2:

另一种可能性是使用主页内的链接http://www.anbima.com.br/reune/reune.asp

当我将代码更改为这个时,我遇到了另一个错误

from selenium import webdriver

path_phantom = 'C:\\Users\\TBMEPYG\\AppData\\Local\\Continuum\\Anaconda3\\Lib\\site-packages\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe'
driver = webdriver.PhantomJS(executable_path= path_phantom)
driver.get('http://www.anbima.com.br/reune/reune.asp')


data = driver.find_element_by_name('Dt_Ref')
data.clear()
data.send_keys('21/08/2017')

错误:

Traceback (most recent call last):
  File "C:\Users\TBMEPYG\Desktop\vamo.py", line 9, in <module>
    data = driver.find_element_by_name('Dt_Ref')
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 426, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 832, in find_element
    'value': value})['value']
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "C:\Users\TBMEPYG\AppData\Local\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: {"request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"89","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:61820","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"name\", \"value\": \"Dt_Ref\", \"sessionId\": \"e61dd170-8dcf-11e7-a019-41573671066b\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/e61dd170-8dcf-11e7-a019-41573671066b/element"}}
Screenshot: available via screen

【问题讨论】:

  • 不是表格,是表格
  • @Hackerman 知道如何与该表进行交互吗?检查元素让我相信它们都是表单,但我在 javascript 世界中真的是新手。
  • 试试,data = driver.find_element_by_name('Dt_Ref')
  • @Hackerman 我已经这样做了。我相信我在 html 代码中遗漏了一些东西,因为 dt_ref 没有出现在那里,只出现在 chrome 的检查中。按照你的建议,我遇到了这个错误:selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with name 'dt_ref'"

标签: javascript python forms selenium web-scraping


【解决方案1】:

为了能够处理form 中的元素,您需要先切换到iframe

driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@class="full"]'))
data = driver.find_element_by_name('Dt_Ref')
data.clear()
data.send_keys('21/08/2017')

【讨论】:

  • 我相信我已经通过另一种方式解决了这一步。我找到了“第二页”anbima.com.br/reune/reune.asp,但仍然无法废弃 Dt_Ref 元素。当我检查此链接的 html 时,所有信息都在那里,但我不能废弃它。之前显示的错误是因为驱动程序找不到元素,但现在这是错误:selenium.common.exceptions.WebDriverExceptio
  • 我的代码对我来说在两个页面上都有效(第二个没有切换)。可以分享完整的异常日志吗?
  • 当然。我编辑了这个问题,因为错误太长,无法在 cmets 上发布。
  • 您确定this 是您要处理的确切页面吗?这似乎是一些测试页面...该页面上没有 iframe
  • 对不起。我添加了错误的页面。这就是我在练习的。
【解决方案2】:

只是为了更新:

问题没有解决,但我知道是什么。

这是 phantomJS 中正在发生的错误。所以,如果您有同样的问题,请尝试使用 Chrome 或 Firefox。

感谢您的回答。

【讨论】:

  • 我想说你的问题已经基本解决了。您应该将 Andersson 的答案标记为已接受的解决方案,因为他帮助您对其进行错误测试并尝试他提供的无头 chrome 链接。
【解决方案3】:

我认为你搞砸了单引号和双引号。正确的代码是-

driver.get("http://www.anbima.com.br/reune/reune.asp")
data = driver.find_element_by_name("Dt_Ref")
data.clear()
data.send_keys("21/08/2017")

【讨论】:

  • 如何解决问题?在 Python 中,无论使用单引号还是双引号
猜你喜欢
  • 1970-01-01
  • 2020-09-29
  • 2022-12-26
  • 2020-08-03
  • 2019-01-31
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
相关资源
最近更新 更多