🌐 AI搜索 & 代理 主页
Skip to content

Commit 1a817bc

Browse files
committed
Modification in web scraping scenarios
1 parent 3de7c15 commit 1a817bc

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

tests/web-scraping/test_async_ecommerce_scrapping.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import sys
44
import ssl
55
import certifi
6+
import time
67
from pprint import pprint
78
from bs4 import BeautifulSoup
89

9-
sys.path.append(sys.path[0] + "/../../..")
10+
sys.path.append(sys.path[0] + "/../../")
1011

1112
from pageobject.locators import locators
1213
from pageobject.helpers import helpers
@@ -25,7 +26,7 @@ async def fetch(url, session):
2526

2627
async def scrap_ecommerce(url):
2728
ssl_context = ssl.create_default_context(cafile=certifi.where())
28-
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl_context=ssl_context)) as session:
29+
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session:
2930
html = await fetch(url, session)
3031
soup = BeautifulSoup(html, 'html.parser')
3132

@@ -48,6 +49,7 @@ async def scrap_ecommerce(url):
4849
return meta_data_arr
4950

5051
async def main():
52+
start_time = time.time()
5153
base_url = locators.test_bs4_url
5254
tasks = [scrap_ecommerce(f"{base_url}&page={i}") for i in range(1, 6)]
5355
results = await asyncio.gather(*tasks)
@@ -57,7 +59,8 @@ async def main():
5759
print("*********************************************************************************************************")
5860
helpers.print_scrapped_content(result)
5961
print()
62+
63+
print("\nTime elapsed is " + str((time.time() - start_time)) + " seconds")
6064

6165
if __name__ == '__main__':
62-
output = asyncio.run(main())
63-
print(output)
66+
output = asyncio.run(main())

tests/web-scraping/test_sync_ecommerce_scrapping.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Import the locators file
44
import sys
55
from pprint import pprint
6-
sys.path.append(sys.path[0] + "/../../..")
6+
sys.path.append(sys.path[0] + "/../../")
77

88
from pageobject.locators import locators
99
from pageobject.locators import *
@@ -44,10 +44,13 @@ def scrap_ecommerce(url) -> list:
4444
# Page 1: https://ecommerce-playground.lambdatest.io/index.php?route=product/category&path=57&page=1
4545
# Page 5: https://ecommerce-playground.lambdatest.io/index.php?route=product/category&path=57&page=5
4646
if __name__ == '__main__':
47+
start_time = time.time()
4748
for iteration in range(1,6):
4849
test_url = locators.test_bs4_url + "&page=" + str(iteration)
4950
meta_data_arr = scrap_ecommerce(test_url)
5051
print('\n')
5152
print("Product Page = " + test_url)
5253
print("*********************************************************************************************************\n")
53-
helpers.print_scrapped_content(meta_data_arr)
54+
helpers.print_scrapped_content(meta_data_arr)
55+
56+
print("\nTime elapsed is " + str((time.time() - start_time)) + " seconds")

0 commit comments

Comments
 (0)