· 6 years ago · Dec 12, 2019, 08:30 AM
1import requests
2from bs4 import BeautifulSoup
3import csv
4import json
5
6def load_id_dict():
7 with open('strings.csv', 'r', newline='') as f:
8 file_data = csv.reader(f, delimiter=',')
9 id_dic = {row[0]: (row[1]) for row in file_data}
10 return id_dic
11
12headers = {
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
15api = requests.get("https://poe.ninja/api/Data/GetCurrencyOverview?league=Standard")
16api_json = api.json()
17#api_str = json.dumps(api_json, indent=2)
18#print(api_str)
19for key in api_json["lines"]:
20 print (key["currencyTypeName"])
21 print (key["chaosEquivalent"])
22
23workingString = ""
24for _, v in load_id_dict().items():
25 URL = 'http://poe.trade/search/' + v
26
27 page = requests.get(URL, headers=headers)
28 soup = BeautifulSoup(page.content, 'html.parser')
29
30 item_name = soup.findAll("tbody", {"id": "item-container-0"})
31 item_prices = soup.findAll("span", {"class": "currency"})
32
33 title = item_name[0]["data-name"]
34
35 prices = []
36 for container in item_prices[:5]:
37 price = container["title"]
38 if "chaos" in price:
39 price = price.replace(' chaos', '')
40 price = int(price)
41 prices.append(price)
42 elif():
43 for key in api_json["lines"]:
44 currency_name = key["currencyTypeName"]
45 exchange_rate = key["chaosEquivalent"]
46 if currency_name in price:
47 print(currency_name, exchange_rate)
48 price = int(price)
49 prices.append(price)
50
51 avgPrice = sum(prices) / len(prices) if prices else "N/A"
52
53 workingString += f"{title},{avgPrice}\n"
54
55print(workingString)
56
57filename = "out.csv"
58with open(filename, "w") as output_file:
59 output_file.write(workingString)