-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
140 lines (125 loc) · 4.86 KB
/
Copy pathapp.py
File metadata and controls
140 lines (125 loc) · 4.86 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import requests
from bs4 import BeautifulSoup
import csv
from pandas.io.excel import ExcelWriter
import pandas
# base url
URL = "https://columbusrealtors.com/find.aspx?mode=browse&letter="
# define session
session = requests.session()
# define header of request
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}
# define file names
csv_file = "result.csv"
xls_file = "result.xlsx"
# define the pageNumber range when page size is 35
maxPagenumber = 235
cnt = 0
# define functions
# getValue
## param soup : beautiful soup Object:
## param eleName : element Name
## param dict : query for element
## return mixed
def getValue(soup, eleName, dict):
try:
ele = soup.find(eleName, dict)
return ele.get('value')
except:
return ""
pass
# savetoCSV
def writeToFile(data, isHeader=False):
if isHeader:
myFile = open(csv_file, 'w', newline='')
else:
myFile = open(csv_file, 'a', newline='')
with myFile:
writer = csv.writer(myFile)
writer.writerow(data)
myFile.close()
# step 1
# get html content for get hidden values
res = session.get(URL)
soup = BeautifulSoup(res.text, "html.parser")
__VIEWSTATE = getValue(soup, "input", {'id':"__VIEWSTATE"})
__VIEWSTATEGENERATOR = getValue(soup, "input", {"id" : "__VIEWSTATEGENERATOR"})
__EVENTVALIDATION = getValue(soup, "input", {"id" : "__EVENTVALIDATION"})
def getPageData(pageNumber):
global __VIEWSTATE, __VIEWSTATEGENERATOR, __EVENTVALIDATION, header, cnt
if pageNumber < 3:
__EVENTTARGET = "ctl00$body$primary_body_1$ctl01$ucSearchResults$radSearchResults$ctl00$ctl02$ctl00$ctl0" + str(5 + pageNumber * 2)
else:
__EVENTTARGET = "ctl00$body$primary_body_1$ctl01$ucSearchResults$radSearchResults$ctl00$ctl02$ctl00$ctl" + str(5 + pageNumber* 2)
data = {
"__EVENTTARGET": __EVENTTARGET,
"__VIEWSTATE": __VIEWSTATE,
"__VIEWSTATEGENERATOR": __VIEWSTATEGENERATOR,
"__EVENTVALIDATION": __EVENTVALIDATION,
}
# get page data of list
res = session.post(URL, data=data,headers=header)
soup1 = BeautifulSoup(res.text, "html.parser")
__VIEWSTATE = getValue(soup1, "input", {'id': "__VIEWSTATE"})
__VIEWSTATEGENERATOR = getValue(soup1, "input", {"id": "__VIEWSTATEGENERATOR"})
__EVENTVALIDATION = getValue(soup1, "input", {"id": "__EVENTVALIDATION"})
data = {
"__EVENTTARGET": "",
"__VIEWSTATE": __VIEWSTATE,
"__VIEWSTATEGENERATOR": __VIEWSTATEGENERATOR,
"__EVENTVALIDATION": __EVENTVALIDATION,
}
dataTable = soup1.find("table", {"id" : "ctl00_body_primary_body_1_ctl01_ucSearchResults_radSearchResults_ctl00"}).find_all("tbody")[2]
for tr in dataTable.find_all('tr'):
# first name, last name, company
lastName = tr.find_all("td")[1].text
firstName= tr.find_all("td")[2].text
company = tr.find_all("td")[3].text
city = tr.find_all("td")[4].text
type = tr.find_all("td")[5].text.replace('\r\n\t\t\t\t\t\t\t\t','').replace('\r\n\t\t\t\t\t\t\t','')
# get other data (address, Phone, Fax, Email Address) from detail page
href = tr.find('a').get('href')
data.update({"__EVENTTARGET": href.replace("javascript:__doPostBack('", "").replace("','')", "")})
# get detail data
res1 = session.post(URL, data=data, headers=header)
soup2 = BeautifulSoup(res1.text, "html.parser")
dataDiv = soup2.find_all("div", {"class": "island"})[1]
ptags = dataDiv.find_all("p")
#address
try:
text = ptags[0].text
address = text.replace('\r\n\t\t\t\t','').replace(company, '').replace('\r\n\t\t\t','').replace('\n','')
except:
address = ""
pass
#phone
phone = ptags[1].text.replace('Phone:','').replace('\n ','')
#fax
fax = ptags[2].text.replace('Fax:','').replace('\n ','')
#email
email = ptags[3].text.replace('Email:','').replace('\n ','')
dt = [lastName, firstName, company, city, type, address, phone, fax, email]
cnt += 1
print(cnt)
# save one data to csv
writeToFile(dt)
csvHeader = ["Last Name", "First Name", "Company", "City", "Type", "Address", "Phone", "Fax", "Email Address"]
writeToFile(csvHeader, True)
pageNumber = 0
isFirst = True
for number in range(0, maxPagenumber):
if isFirst == True and pageNumber <= 10:
pageNumber = pageNumber
elif isFirst == False and pageNumber <= 11:
pageNumber = pageNumber
else:
isFirst = False
pageNumber = pageNumber % 10
print("pageNumber" + str(pageNumber))
getPageData(pageNumber)
pageNumber += 1
# convert csv file to excel format
with ExcelWriter(xls_file) as ew:
df = pandas.read_csv(csv_file)
df.to_excel(ew, sheet_name="sheet1", index=False)