简体   繁体   English

使用python硒获取网站表数据时出错-多个表且无法找到元素

[英]Error when getting website table data using python selenium - Multiple tables and Unable to locate element

I am trying to get info from brazilian stock market (BMF BOVESPA). 我正在尝试从巴西股票市场(BMF BOVESPA)获取信息。 The website has several tables, but my code is not being able to get them. 该网站有几个表,但是我的代码无法获取它们。

The code below aims to get all data from table "Ações em Circulação no Mercado" -> one of the last tables from webpage. 下面的代码旨在从表“AçõesemCirculaçãono Mercado”中获取所有数据->该网页中的最后一个表。

I have tried the ones below, but none worked for me: 我尝试了以下方法,但没有一个适合我:

content = browser.find_element_by_css_selector('//div[@id="div1"]') 内容= browser.find_element_by_css_selector('// div [@ id =“ div1”]')

and

table = browser.find_element_by_xpath(('//*[@id="div1"]/div/div/div 1 /table/tbody')) 表= browser.find_element_by_xpath(('// * [@ id =“ div1”] / div / div / div 1 / table / tbody'))

Thanks in advance for taking my question. 预先感谢您提出我的问题。

from selenium import webdriver
from time import sleep

url = "http://bvmf.bmfbovespa.com.br/cias-Listadas/Empresas-
Listadas/ResumoEmpresaPrincipal.aspx?codigoCvm=19348&idioma=pt-br"
browser = webdriver.Chrome()
browser.get(url)
sleep(5) #wait website to reload
content = browser.find_element_by_css_selector('//div[@id="div1"]')

HTML can be found at attached picture 可以在所附图片中找到HTML

HTML

As alternative, the code below reaches the same website 另外,下面的代码也可以到达同一网站

url = "http://bvmf.bmfbovespa.com.br/cias-Listadas/Empresas-Listadas/BuscaEmpresaListada.aspx?idioma=pt-br"
Ticker='ITUB4'
browser = webdriver.Chrome()
browser.get(url)
sleep(2)
browser.find_element_by_xpath(('//*[@id="ctl00_contentPlaceHolderConteudo_BuscaNomeEmpresa1_txtNomeEmpresa_txtNomeEmpresa_text"]')).send_keys(Ticker)
browser.find_element_by_xpath(('//*[@id="ctl00_contentPlaceHolderConteudo_BuscaNomeEmpresa1_btnBuscar"]')).click();
content = browser.find_element_by_id('div1')

Selenium with Python documentation UnOfficial Selenium与Python文档非官方

Hii there 那里的Hii

Selenium provides the following methods to locate elements in a page: Selenium提供了以下方法来查找页面中的元素:

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

Why your code doesnt work ? 为什么您的代码不起作用? because you're not using correct correct code to locate element 因为您没有使用正确的正确代码来定位元素

you're using xpath inside css selector 您正在CSS选择器中使用xpath

content = browser.find_element_by_css_selector('//div[@id="div1"]') #this part is wrong

instead you can do this if you want to select div1 相反,如果您要选择div1,则可以执行此操作

content = browser.find_element_by_id('div1')

here's the correct code 这是正确的代码

url = "http://bvmf.bmfbovespa.com.br/cias-Listadas/Empresas-

Listadas/BuscaEmpresaListada.aspx?idioma=pt-br"
Ticker='ITUB4'
browser = webdriver.Chrome()
browser.get(url)
sleep(2)
browser.find_element_by_xpath(('//*[@id="ctl00_contentPlaceHolderConteudo_BuscaNomeEmpresa1_txtNomeEmpresa_txtNomeEmpresa_text"]')).send_keys(Ticker)
browser.find_element_by_xpath(('//*[@id="ctl00_contentPlaceHolderConteudo_BuscaNomeEmpresa1_btnBuscar"]')).click()

I tested it and it worked :) 我测试了它,它起作用了:)

Mark it as best answer if i helped you :) 如果我帮助过您,请将其标记为最佳答案:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM