-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrueblue.py
68 lines (56 loc) · 1.91 KB
/
trueblue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 6 09:41:48 2020
@author: huyng
"""
import pandas as pd
import requests as rq
from bs4 import BeautifulSoup as bs
from pathlib import Path
URL_PERNAMENT = r'https://truebluewaterexchange.com/permanent-water/'
URL_TEMPORARY = r'https://truebluewaterexchange.com/temporary-water/'
OUTPUT_PATH = Path.cwd() / 'output' / 'trueblue'
OUTPUT_PATH.mkdir(parents=True, exist_ok=True)
if __name__ == '__main__':
# Get Pernament
soup = bs(rq.get(URL_PERNAMENT).text, 'lxml')
soup = soup.find('div', attrs={'class':'rtd'})
soup = soup.find_all('div',recursive=False)
df = []
cur_category = ''
for s in soup:
category = s.find_all('h2')
content = s.find_all('p')
if category:
cur_category = category[0].text
if content and len(content) == 4:
row = [c.text for c in content]
row.append(cur_category)
df.append(row)
df = pd.DataFrame(df)
df.columns = df.iloc[0,:]
df = df.iloc[1:,:]
df.columns = [*df.columns[:-1], 'Buyers/Sellers?']
df.to_csv(OUTPUT_PATH / 'pernament.csv')
# Get Temporary
soup = bs(rq.get(URL_TEMPORARY).text, 'lxml')
soup = soup.find('div', attrs={'class':'rtd'})
soup = soup.find_all('div',recursive=False)
df = []
cur_category = ''
saved_column = []
for s in soup:
category = s.find_all('h2')
content = s.find_all('p')
column = s.find_all('h4')
if category:
cur_category = category[0].text
elif content and len(content) == 4:
row = [c.text for c in content]
row.append(cur_category)
df.append(row)
elif column and len(column) == 4:
saved_column = [c.text for c in column]
df = pd.DataFrame(df)
df.columns = saved_column + ['Buyers/Sellers?']
df.to_csv(OUTPUT_PATH / 'temporary.csv')