Advertisement
Guest User

w/o comments

a guest
Dec 11th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import csv
  4.  
  5. def load_id_dict():
  6.     with open('strings.csv', 'r', newline='') as f:
  7.         file_data = csv.reader(f, delimiter=',')
  8.         id_dic = {row[0]: (row[1]) for row in file_data}
  9.     return id_dic
  10.  
  11. headers = {
  12.         "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
  13.  
  14. workingString = ""
  15. for _, v in load_id_dict().items():
  16.     URL = 'http://poe.trade/search/' + v
  17.  
  18.     page = requests.get(URL, headers=headers)
  19.     soup = BeautifulSoup(page.content, 'html.parser')
  20.  
  21.     item_name = soup.findAll("tbody", {"id": "item-container-0"})
  22.     item_prices = soup.findAll("span", {"class": "currency"})
  23.    
  24.     title = item_name[0]["data-name"]
  25.    
  26.     prices = []
  27.     for container in item_prices[:5]:
  28.         price = container["title"]
  29.         if "chaos" in price:
  30.             price = price.replace(' chaos', '')
  31.             price = int(price)
  32.             prices.append(price)
  33.            
  34.    
  35.     avgPrice = sum(prices) / len(prices) if prices else "N/A"
  36.  
  37.     workingString += f"{title},{avgPrice}\n"
  38.    
  39. print(workingString)
  40.  
  41. filename = "out.csv"
  42. with open(filename, "w") as output_file:
  43.     output_file.write(workingString)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement