Issue
I´m trying to make an easy scraper with python for ebay, the thing is I can´t use the dataframe I make. My code:
import requests
from bs4 import BeautifulSoup
import pandas as pd
from csv import reader
url = "https://www.ebay.es/sch/i.html?_from=R40&_nkw=iphone&_sacat=0&LH_TitleDesc=0&_fsrp=1&Modelo=Apple%2520iPhone%2520X&_dcat=9355"
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
productslist = []
results = soup.find_all('div', {'class': 's-item__info clearfix'})
print(len(results))
for item in results:
product = {
'title': item.find('h3', {'class': 's-item__title'}),
'soldprice': item.find('span', {'class': 's-item__price'})
}
productslist.append(product)
df = pd.DataFrame(productslist)
df
But the dataframe I get it´s like this: Dataframe
I would like to be able to work with the numbers of the price but I can´t use it, I imagine it´s because dtype: object, I would like to know how to convert for example [359,00 EUR] in 359,00 to be able to make graphics. Thanks.
Solution
To remove the "price" from a [...]
format, use the .text
method. Also, make sure that the price not None
:
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = "https://www.ebay.es/sch/i.html?_from=R40&_nkw=iphone&_sacat=0&LH_TitleDesc=0&_fsrp=1&Modelo=Apple%2520iPhone%2520X&_dcat=9355"
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
productslist = []
results = soup.find_all("div", {"class": "s-item__info clearfix"})
for item in results:
title = item.find("h3", {"class": "s-item__title"})
price = item.find("span", {"class": "s-item__price"})
if price is None:
continue
productslist.append({"title": title.text, "soldprice": price.text})
df = pd.DataFrame(productslist)
print(df)
Output:
title soldprice
0 APPLE IPHONE X 64 GB A+LIBRE+FACTURA+8 ACCESOR... 359,00 EUR
1 Apple iPhone X - 64GB - Plata (Libre) 177,50 EUR
2 Apple iPhone X - 64GB - Blanco (Libre) 181,50 EUR
3 iphone x 64gb 240,50 EUR
4 Iphone x 256 gb 370,00 EUR
5 Apple iPhone X - 256GB - Space Gray (Libre) 400,00 EUR
6 Nuevo anuncioSMARTPHONE APPLE IPHONE X 64GB LI... 334,95 EUR
...
...
Answered By - MendelG
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.