-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_search_asins.py
More file actions
167 lines (148 loc) · 6.48 KB
/
Copy pathget_search_asins.py
File metadata and controls
167 lines (148 loc) · 6.48 KB
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
import random
import re
import time
from itertools import cycle
from threading import Thread
import requests
from lxml.html import fromstring
working_ip = []
proxy_pool = []
def get_proxies(): # getting proxies by scrapping the site for free
url3 = 'https://free-proxy-list.net/'
response = requests.get(url3)
parser = fromstring(response.text)
proxies = set()
for i in parser.xpath('//tbody/tr')[:200]:
if i.xpath('.//td[7][contains(text(),"yes")]'):
proxy = ":".join([i.xpath('.//td[1]/text()')[0], i.xpath('.//td[2]/text()')[0]])
proxies.add(proxy)
print(proxies)
return proxies
def find_ip(lower_range, upper_range):
# finds ip in a given range from an ip list generated from get_pro_proxie or get_proxie
url4 = 'https://httpbin.org/ip'
session = requests.Session()
for i in range(lower_range, upper_range):
# Get a proxy from the pool
proxy = next(proxy_pool)
print("Request #%d" % i)
try:
response = session.get(url4, proxies={"http": proxy, "https": proxy}, timeout=10)
print(response.json())
print(proxy)
working_ip.append(proxy)
except:
# Most free proxies will often get connection errors. You will have retry the entire request using
# another proxy to work. We will just skip retries as its beyond the scope of this tutorial and we are
# only downloading a single url
print("Skipping. Connnection error")
def search_amazon(front_url):
deals_asin = []
global proxy_pool
proxies = get_proxies()
proxy_pool = cycle(proxies)
print('Finding viable ip address for proxy...')
ip_threads = [] # lists of threads to join
high_i = 2
low_i = 1
while low_i < 76: # change number here for num ips checked
ip_thread = Thread(target=find_ip, args=(low_i, high_i))
ip_threads.append(ip_thread)
ip_thread.start()
low_i = low_i + 1
high_i = high_i + 1
time.sleep(0.1)
for t in ip_threads:
t.join() # joins all started threads to find working ups
print("@@@@@@ getting deals of the day page @@@@@@")
product_headers = [
{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Referer": "https://www.google.com/",
"DNT": "1",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1"
},
# Firefox 77 Windows
{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://www.google.com/",
"DNT": "1",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1"
},
# Chrome 83 Mac
{
"Connection": "keep-alive",
"DNT": "1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Sec-Fetch-Site": "none",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Dest": "document",
"Referer": "https://www.google.com/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8"
},
# Chrome 83 Windows
{
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-User": "?1",
"Sec-Fetch-Dest": "document",
"Referer": "https://www.google.com/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9"
}]
# Create a request session
site_response = requests.Session()
# Download the page using requests
print("Downloading %s" % front_url)
current_ip = random.randint(0, len(working_ip) - 1)
print('current proxy ' + working_ip[current_ip])
stop_count = 0
headers = random.choice(product_headers)
r = ''
while r == '':
try:
r = site_response.get(front_url, headers=headers, timeout=45, proxies={"http": working_ip[current_ip],
"https": working_ip[current_ip]})
response = str(r.text)
if "data-asin=" not in response:
print('Amazon blocked so new ip')
print(r.text)
current_ip = random.randint(0, len(working_ip) - 1)
headers = random.choice(product_headers)
r = ''
else:
break
except:
sleep_time = 5
print("Connection refused by the server..")
print("Let me sleep for " + str(sleep_time) + " seconds")
print("ZZzzzz...")
time.sleep(sleep_time)
print("Was a nice sleep, now let me continue...")
stop_count = stop_count + 1
print("stop count " + str(stop_count))
if stop_count > 3:
current_ip = random.randint(0, len(working_ip) - 1) # try to assign this to the global ip array
print('to many stops, reassigning randint')
stop_count = 0
continue
response = str(r.text)
result = re.findall('data-asin="(.*)" data-index', response)
final_asins = list(filter(None, result))
return final_asins
if __name__ == "__main__":
search_amazon('https://www.amazon.com/s?k=dorm+products&i=electronics&ref=nb_sb_noss_1')