forked from fgscivittaro/ebay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_data.py
203 lines (164 loc) · 6.61 KB
/
collect_data.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from scrape_page import *
from collect_links import *
import schedule
def open_new_file(filename):
"""
Opens a new data file with a specified title; if the title already exists,
then the function will overwrite the old file and create a new file with
column headers.
"""
with open(filename, "w") as initial_file:
initial_file.write(
"Time" + ";" +
"Item ID" + ";" +
"Title" + ";" +
"Condition" + ";" +
"Trending Price ($)" + ";" +
"List price ($)" + ";" +
"Product Discount ($)" + ";" +
"Product Discount (%)" + ";" +
"Current Price ($)" + ";" +
"Shipping Cost ($)" + ";" +
"Item Location" + ";" +
"Delivery Date" + ";" +
"Return Policy" + ";" +
"Total Ratings" + ";" +
"Product Rating (0.0-5.0)" + ";" +
"Username" + ";" +
"Seller Reviews" + ";" +
"Seller Feedback (%)" + ";" +
"Hot Info" + ";" +
"Users Watching" + ";" +
"Amount Sold" + ";" +
"Percent Sold (%)" + ";" +
"Amount Available" + ";" +
"Inquiries" + ";" +
"First Reason" + ";" +
"Second Reason" + ";" +
"Third Reason" + ";" +
"Date" + ";" +
"URL" + "\n"
)
print "New file named %s created" % (filename)
def append_data(filename, product_dict):
"""
Appends already-scraped data to an already-existing file.
"""
with open(filename, "a") as datafile:
datafile.write(
product_dict['Time'] + ";" +
product_dict['Item ID'] + ";" +
product_dict['Title'] + ";" +
product_dict['Condition'] + ";" +
product_dict['Trending Price'] + ";" +
product_dict['List Price'] + ";" +
product_dict['Product Discount ($)'] + ";" +
product_dict['Product Discount (%)'] + ";" +
product_dict['Current Price'] + ";" +
product_dict['Shipping Cost'] + ";" +
product_dict['Item Location'] + ";" +
product_dict['Delivery Date'] + ";" +
product_dict['Return Policy'] + ";" +
product_dict['Total Ratings'] + ";" +
product_dict['Product Rating'] + ";" +
product_dict['Username'] + ";" +
product_dict['Seller Reviews'] + ";" +
product_dict['Seller Feedback'] + ";" +
product_dict['Hot Info'] + ";" +
product_dict['Users Watching'] + ";" +
product_dict['Amount Sold'] + ";" +
product_dict['Percent Sold'] + ";" +
product_dict['Amount Available'] + ";" +
product_dict['Inquiries'] + ";" +
product_dict['First Reason'] + ";" +
product_dict['Second Reason'] + ";" +
product_dict['Third Reason'] + ";" +
product_dict['Date'] + ";" +
product_dict['URL'] + "\n"
)
print "Data appended"
def scrape_and_append_data(url, filename, num_retries = 10):
"""
Scrapes data from a desired eBay product page and appends it to an
already-existing file.
"""
product_dict = find_all_product_info(url, num_retries)
print "Data scraped"
append_data(filename, product_dict)
def scrape_all_data_from_all_featured_products(filename,
link_list,
num_retries = 10):
"""
Takes a list of featured eBay product links, scrapes all information for
each link, and appends it to a file.
"""
for url in link_list:
scrape_and_append_data(url, filename, num_retries)
print "Finished appending data for all links"
def write_new_file_and_scrape_all_data(filename, link_list, num_retries = 10):
"""
Writes a new file, scrapes data from every product link in a list, and
appends each product's data to the previously created file.
"""
open_new_file(filename)
scrape_all_data_from_all_featured_products(filename, link_list, num_retries)
def dynamically_scrape_data(filename, link_list, interval, num_retries = 10):
"""
Repeatedly runs the scraper every time the specified interval has passed
and continuously appends the data to a file.
"""
def job():
scrape_all_data_from_all_featured_products(filename,
link_list,
num_retries)
job()
schedule.every(interval).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(30)
print "Dynamic scraping finished"
def write_new_file_and_dynamically_scrape_all_data(filename,
link_list,
interval,
num_retries = 10):
"""
Writes a new file and repeatedly runs the scraper every time the specified
interval has passed and continuously appends the data to a file.
"""
open_new_file(filename)
dynamically_scrape_data(filename, link_list, num_retries, interval)
def clean_links_and_scrape(filename, num_retries = 10):
"""
Cleans the link list by checking for and removing bad links. Once the bad
links have been removed, the function scrapes the clean list and appends the
data to a file.
"""
link_list = collect_all_featured_links()
bad_links = collect_bad_links(link_list)
clean_links = remove_bad_links_from_link_list(bad_links, link_list)
scrape_all_data_from_all_featured_products(filename,
clean_links,
num_retries)
def clean_links_and_dynamically_scrape(filename, interval, num_retries = 10):
"""
Repeatedly updates the link list and runs the scraper every time the
specified interval has passed and continuously appends the data to a file.
"""
def job():
clean_links_and_scrape(filename, num_retries)
job()
schedule.every(interval).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(30)
print "Dynamic scraping finished"
def write_new_file_update_links_and_dynamically_scrape(filename,
interval,
num_retries = 10):
"""
Writes a new file and repeatedly updates the link list and runs the scraper
every time the specified interval has passed and continuously appends the
data to a file.
"""
open_new_file(filename)
clean_links_and_dynamically_scrape(filename, interval, num_retries)