-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
67 lines (47 loc) · 2.04 KB
/
test.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
from time import time
import aiohttp
import asyncio
import requests
from bs4 import BeautifulSoup
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' }
with open('./items.txt') as f:
URLs = f.read().strip().split('\n')
s = time()
async def check_price(session, url):
async with session.get(url) as resp:
page = await resp.read()
soup = BeautifulSoup(page, 'html.parser')
# for flipkart
title = soup.find("span", {"class": "B_NuCI"}).get_text()
price = soup.find("div", {"class": "_30jeq3 _16Jk6d"}).get_text()[1:].replace(',','')
# for amazon
# title = soup.find("span", {"id": "productTitle"}).get_text()
# price = soup.find("span", {"class": "a-offscreen"}).get_text()[1:].replace(',','')
return price,title #prints the price
async def main():
async with aiohttp.ClientSession() as session:
tasks = [asyncio.ensure_future(check_price(session, url)) for url in URLs]
values = await asyncio.gather(*tasks)
return values
def test():
return asyncio.run(main())
def check_price_flipkart(url):
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find("span", {"class": "B_NuCI"}).get_text()
price = soup.find("div", {"class": "_30jeq3 _16Jk6d"}).get_text()[1:].replace(',','')
print(price,title) #prints the price
# check_price_flipkart()
def check_price_amazon():
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find("span", {"id": "productTitle"}).get_text()
price = soup.find("span", {"class": "a-offscreen"}).get_text()[1:].replace(',','')
print(price,title) #prints the price
# check_price_amazon()
s= time()
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # only for windows
aa = asyncio.run(main())
print(aa)
# print("--- %s seconds ---" % (time.time() - start_time))
print(time()-s)