-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps_parse.py
73 lines (52 loc) · 1.83 KB
/
ps_parse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding=utf-8 -*-
#!/usr/bin/env python
import requests
from bs4 import BeautifulSoup
import os
import pandas as pd
import numpy as np
def parse(item):
# Adapted from https://www.blog.datahut.co/post/scraping-ebay
items = []
prices = []
url = "https://www.ebay.com/sch/i.html?_nkw={0}&_sacat=0".format(
item.replace(" ", "+")
)
r = requests.get(url)
soup = BeautifulSoup(r.text, features="lxml")
listings = soup.find_all("li", attrs={"class": "s-item"})
for l in listings:
item_name = " "
item_price = " "
for n in l.find_all("h3", attrs={"class": "s-item__title"}):
# Find item names
if str(n.find(text=True, recursive=False)) != "None":
item_name = str(n.find(text=True, recursive=False))
items.append(item_name)
# Find item price
if item_name != " ":
item_price = l.find("span", attrs={"class": "s-item__price"})
item_price = str(item_price.find(text=True, recursive=False))
item_price = item_price.replace("$", "")
try:
item_price = float(item_price)
except:
# print("ERROR: parsing price")
item_price = np.nan
prices.append(item_price)
# Put into df
df = pd.DataFrame({"name": items, "price": prices, "item": item})
return df
def build_price_df(
file_name="ps_prices.csv", levels=[1, 2, 3, 4], save_to_file=True, rm_na=False
):
# Find average prices of each level of playstation
df = pd.DataFrame()
for l in levels:
s = "Playstation " + str(l)
df = df.append(parse(str(s)))
if save_to_file:
df.to_csv(file_name, index=False)
if rm_na:
df = df.dropna()
return df