Advertisement
Sushill

Untitled

Jul 8th, 2023
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. import aiohttp
  2. from tool import userAgents
  3. import logging
  4.  
  5. import asyncio
  6. from bs4 import BeautifulSoup
  7.  
  8. async def main():
  9.     url = "https://www.amazon.se/s?k=intel"
  10.     headers = {
  11.         "User-Agent": userAgents()
  12.     }
  13.  
  14.     async with aiohttp.ClientSession() as session:
  15.         try:
  16.             async with session.get(url, proxies=proxy, headers=headers) as response:
  17.                 html = await response.text()
  18.  
  19.                 if amazon.useProxy and not response.connection.proxy_manager:
  20.                     logging.error(
  21.                         build_log_message(
  22.                             colored("request gjordes utan proxy. Stänger ner....", "red")
  23.                         )
  24.                     )
  25.                     return None
  26.                 soup = BeautifulSoup(html, "html.parser")
  27.  
  28.                 products = soup.select_one("div[data-component-type='s-search-result']")
  29.                 for product in products:
  30.                     title = product.select_one("a.a-link-normal.a-text-normal").text
  31.                     price = product.select_one("span.a-price").text
  32.  
  33.                     print(f"Title: {title}")
  34.                     print(f"Price: {price}")
  35.         except aiohttp.ClientError as e:
  36.             logging.error(build_log_message("Inget svar vid requests..."))
  37.             return None
  38.  
  39. # Call the `main` function to run the code asynchronously
  40. asyncio.run(main())
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement