Reputation: 131
I have scraped data from a table, and need help to export the data to excel. I have created a dataframe after my loop, but when I open the excel file, I can see that the data is not there. Instead, it seems like html or json links. Here is my code, and at the end I will show the output.
import time
import requests
from lxml import html
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
url = "https://coinmarketcap.com/"
driver = webdriver.Chrome(executable_path=r'C:\Users\Ejer\PycharmProjects\pythonProject\chromedriver.exe')
driver.get(url)
driver.maximize_window()
for i in range(22):
coin_name = driver.find_elements_by_xpath('//td[3]/a[@class="cmc-link" and starts-with(@href, "/currencies/")]')
prices = driver.find_elements_by_xpath('//td[4]/div/a[@class = "cmc-link" and contains(@href, "/markets/")]')
market_caps = driver.find_elements_by_xpath('//td[7]/p[@class="sc-1eb5slv-0 kDEzev"]')
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)
for coin, price, cap in zip(coin_name, prices, market_caps):
print("Coin :", coin.text, "Price :", price.text, "Market cap:", cap.text)
print(len(prices))
print(len(coin_name))
print(len(market_caps))
print(type(prices))
print(type(coin_name))
print(type(market_caps))
df = pd.DataFrame({
'Alt coin name': coin_name,
'Price': prices,
'Market cap': market_caps
})
df.to_excel (r'C:\Users\Ejer\PycharmProjects\pythonProject\cmc_data.xlsx', index = False, header=True)
This is an example of the output in excel:
<selenium.webdriver.remote.webelement.WebElement (session="3d7dedd2b323c6bf08db19d694cb9d7f", element="45fa63ec-8e55-4058-b714-683b96a0cc44")>
Upvotes: 0
Views: 66
Reputation: 25048
What is going on?
You try to push the elements directly into your pd / excel, but what you want to push are the values.
So instead of printing all the values in your loop, you could use it to put them in a list
of dicts
and create your df
from that results.
This is doing the trick:
Create your list
of dicts
:
results = [{"Alt coin name": coin.text, "Price": price.text, "Market cap": cap.text} for coin, price, cap in zip(coin_name, prices, market_caps)]
Create your df
:
df = pd.DataFrame(results)
Example
import time
import requests
from lxml import html
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
url = "https://coinmarketcap.com/"
driver = webdriver.Chrome(executable_path=r'C:\Program Files\ChromeDriver\chromedriver.exe')
driver.get(url)
driver.maximize_window()
for i in range(22):
coin_name = driver.find_elements_by_xpath('//td[3]/a[@class="cmc-link" and starts-with(@href, "/currencies/")]')
prices = driver.find_elements_by_xpath('//td[4]/div/a[@class = "cmc-link" and contains(@href, "/markets/")]')
market_caps = driver.find_elements_by_xpath('//td[7]/p[@class="sc-1eb5slv-0 kDEzev"]')
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)
results = [{"Alt coin name": coin.text, "Price": price.text, "Market cap": cap.text} for coin, price, cap in zip(coin_name, prices, market_caps)]
df = pd.DataFrame(results)
df.to_excel (r'C:\Users\Ejer\PycharmProjects\pythonProject\cmc_data.xlsx', index = False, header=True)
Upvotes: 1