如何使用Selenium Python从表中捕获数据?
我需要从链接中捕获表:
https://fr.tradingeconomics.com/country-list/rating
我尝试了以下代码,但我没有得到任何响应,
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
my_url= "https://fr.tradingeconomics.com/country-list/rating"
driver.get(my_url)
#actions = ActionChains(driver)
WebDriverWait(driver, 50).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "table table-hover")))
trs = driver.find_elements(By.TAG_NAME, "tr")
print(len(trs))
countries = []
for tr in trs:
country = {}
items= tr.find_elements(By.TAG_NAME, "td")
for item in items:
country_name = item.find_element(By.XPATH, "//*[@id='ctl00_ContentPlaceHolder1_ctl01_GridView1']/tbody/tr[2]/td[1]")
country['country_name'] = country_name.get_attribute('text')
s_and_p = item.find_element(By.XPATH, "//*[@id='ctl00_ContentPlaceHolder1_ctl01_GridView1']/tbody/tr[2]/td[2]")
country['S&P']= s_and_p.get_attribute("text")
moodys = item.find_element(By.XPATH, "//*[@id='ctl00_ContentPlaceHolder1_ctl01_GridView1']/tbody/tr[2]/td[3]")
country['Moody\'s'] = moodys.get_attribute("text")
countries.append(country)
print(country)
任何帮助将不胜感激。谢谢。
I need to capture the table from the link:
https://fr.tradingeconomics.com/country-list/rating
I tried the following code but I don't get any response
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
my_url= "https://fr.tradingeconomics.com/country-list/rating"
driver.get(my_url)
#actions = ActionChains(driver)
WebDriverWait(driver, 50).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "table table-hover")))
trs = driver.find_elements(By.TAG_NAME, "tr")
print(len(trs))
countries = []
for tr in trs:
country = {}
items= tr.find_elements(By.TAG_NAME, "td")
for item in items:
country_name = item.find_element(By.XPATH, "//*[@id='ctl00_ContentPlaceHolder1_ctl01_GridView1']/tbody/tr[2]/td[1]")
country['country_name'] = country_name.get_attribute('text')
s_and_p = item.find_element(By.XPATH, "//*[@id='ctl00_ContentPlaceHolder1_ctl01_GridView1']/tbody/tr[2]/td[2]")
country['S&P']= s_and_p.get_attribute("text")
moodys = item.find_element(By.XPATH, "//*[@id='ctl00_ContentPlaceHolder1_ctl01_GridView1']/tbody/tr[2]/td[3]")
country['Moody\'s'] = moodys.get_attribute("text")
countries.append(country)
print(country)
Any help would be appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于URL不是动态的,因此您还可以仅使用Pandas轻松地获取
表数据
。输出:
As the url isn't dynamic, so you also can easily grab
table data
using pandas only.Output:
您必须使用
innertext
不是text
,也是第一个tr
没有td
,这是您不是得到任何回应。硒解决方案:
代码:
进口:
You have to use
innerText
nottext
, also the firsttr
does not havetd
that's the reason you are not getting anything in response.Selenium solution:
Code:
Imports: