Advertisement
Guest User

w/ comments

a guest
Dec 11th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import csv
  4.  
  5. def load_id_dict(): ## renamed to dict, more common abbreviation
  6.     with open('strings.csv', 'r', newline='') as f:
  7.         file_data = csv.reader(f, delimiter=',') ## renamed so we aren't reusing names (i.e. reader), even though technically they wouldn't conflict
  8.         id_dic = {row[0]: (row[1]) for row in file_data}
  9.     return id_dic
  10.  
  11. ## moved headers outside loop, doesn't need to be initialized every time
  12. headers = {
  13.         "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'}
  14.  
  15. workingString = ""
  16. for _, v in load_id_dict().items(): ## k isn't currently used, so use _ instead, but we still have the option to use it later if we want to
  17.     URL = 'http://poe.trade/search/' + v ## removed redundant brackets
  18.  
  19.     page = requests.get(URL, headers=headers)
  20.     soup = BeautifulSoup(page.content, 'html.parser')
  21.  
  22.     item_name = soup.findAll("tbody", {"id": "item-container-0"})
  23.     item_prices = soup.findAll("span", {"class": "currency"})
  24.    
  25.     title = item_name[0]["data-name"] ## no need to loop over all the items, they should all
  26.                                       ## have the same name, so just use the first one
  27.     prices = []
  28.     ## removed foundChaos
  29.     for container in item_prices[:5]:
  30.         price = container["title"] ## moved this statement to before the if
  31.         if "chaos" in price: ## simplified
  32.             price = price.replace(' chaos', '')
  33.             price = int(price)
  34.             prices.append(price)
  35.            
  36.    
  37.     avgPrice = sum(prices) / len(prices) if prices else "N/A" ## make sure prices isn't empty
  38.  
  39.     ## use += instead of =
  40.     ## workingString += title + "," + str(avgPrice) + "\n"
  41.     ## but also use f-strings
  42.     workingString += f"{title},{avgPrice}\n"
  43.    
  44.  
  45. print(workingString)
  46.  
  47. filename = "out.csv"
  48. with open(filename, "w") as output_file:
  49.     output_file.write(workingString)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement