【发布时间】:2018-07-01 07:40:51
【问题描述】:
我正在尝试从该网站https://coinmunity.co/ 下载表格,然后使用 Pandas 以简单的方式操作数据。 问题是该表是动态生成的,因此我无法轻松理解其结构或检测我需要执行循环工作的“tr”。 之前用过Requests和BeautifulSoup都试过了,没用,所以这里有人推荐了Selenium,但没有告诉我更多。
在 Selenium 上,我已经尝试了很多东西,包括 xpaths、css 选择器等……但没有任何效果。我的想法是为每一行有序地提取数据,但是行名似乎有一个很奇怪的名字,包括“_ngcontent”,我无法理解。
这是我的(不起作用的)代码:
from selenium import webdriver
import pandas as pd
import time
from bs4 import BeautifulSoup
driver = webdriver.Firefox()
driver.implicitly_wait(10)
#driver.get("https://coinmunity.co/")
url = 'file:///C:/Users/nique/PycharmProjects/untitled/test1.html'
driver.get(url)
html = driver.page_source.encode('utf-8')
#html = driver.page_source.encode('utf-8')
soup = BeautifulSoup(html, 'lxml')
results = []
symbol_list = []
#items = driver.find_elements_by_class_name('coin-link')
items = driver.find_elements_by_css_selector('.inner-container > table:nth-child(1) > tbody:nth-child(2) > tr:nth-child(2)')
#how_many = driver.find_elements_by_css_selector('html body app-root app-home div.outer-container div.inner-container table tbody tr')
count = 1
for el in range(1,3):
#row = driver.find_elements_by_css_selector('.inner-container > table:nth-child(1) > tbody:nth-child(2) > tr:nth-child((count))')
row = driver.find_elements_by_xpath('/html/body/app-root/app-home/div/div/table/tbody/tr[count]')
symbol = row.find_element_by_class_name('coin-link')
followers = driver.find_elements_by_class_name('stats')[0]
changefollowers = driver.find_elements_by_class_name('stats')[1]
# subscribers = driver.find_elements_by_class_name('stats')[2]
# changesubscribers = driver.find_elements_by_class_name('stats')[3]
# price = driver.find_elements_by_class_name('stats')[4]
# changeprice = driver.find_elements_by_class_name('stats')[5]
count += 1
print(symbol)
# results.append({'Symbol': symbol.text, 'TFollowers': followers.text, 'ChangeFollowers': changefollowers.text,'Subscribers': subscribers.text,'ChangeSubscribers': changesubscribers.text,'Price': price.text, 'ChangePrice': changeprice.text})
print(symbol_list)
print(results)
如何以最简单、最整洁的方式下载这些信息并为 Pandas 做好准备? 谢谢
【问题讨论】:
-
您能否详细说明
extract the data in an orderly fashion for each row的确切含义?您尝试Automate的确切Manual Steps是什么? -
此时我的目标只是以一种我可以用 Pandas 轻松可视化的方式下载数据,所以我的意思是我不想让事情变得过于复杂,主要是因为我不是高级程序员也是。
标签: python selenium dictionary html-table