-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_data.py
151 lines (131 loc) · 4.29 KB
/
user_data.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
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
141
142
143
144
145
146
147
148
149
150
import re
from bs4 import BeautifulSoup
import requests
def extract_link(url):
"""
Creates a BeautifulSoup object from the link
:param url: the link
:return: a BeautifulSoup object equivalent of the url
"""
headers = {"Host": "www.zomato.com",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://www.zomato.com/",
"Connection": "keep-alive"}
while(1):
try:
r = requests.get(url, headers=headers)
except requests.exceptions.Timeout:
continue
break
if r.status_code == 404:
return None
doc = r.content
soup = BeautifulSoup(doc,'html.parser')
with open ('zomata_user_data', "a") as filew:
filew.write('\n\n##\n\n')
ptags = soup.find_all('title')
str = ptags[0].string
re.sub('<title>','',str)
re.sub('</title>','',str)
str = str.strip()
str = str[:-9]
lst = str.split(',')
filew.write(lst[0].strip()+'\n')
if(len(lst)>1):
filew.write(lst[1].strip()+'\n')
ptags = soup.find_all("a", 'item user-tab user-tab-reviews cursor-pointer ')
lis = ptags[0].find_all('div')
str = lis[0].string
re.sub('<div class="ui label">','',str)
re.sub('</div>','',str)
filew.write('Reviews = '+str+'\n')
ptags = soup.find_all("a", 'item user-tab user-tab-blogs cursor-pointer ')
if(ptags):
lis = ptags[0].find_all('div')
str = lis[0].string
re.sub('<div class="ui label">','',str)
re.sub('</div>','',str)
filew.write('Blog posts = '+str+'\n')
ptags = soup.find_all("a", 'item user-tab-follows user-tab cursor-pointer ')
lis = ptags[0].find_all('div')
str = lis[0].string
re.sub('<div class="ui label">','',str)
re.sub('</div>','',str)
filew.write('Followers = '+str+'\n')
ptags = soup.find_all("a", 'item user-tab user-tab-bookmarks cursor-pointer ')
lis = ptags[0].find_all('div')
str = lis[0].string
re.sub('<div class="ui label">','',str)
re.sub('</div>','',str)
re.sub(' | Zomato','',str)
filew.write('Bookmarks = '+str+'\n')
#item user-tab user-tab-beenthere cursor-pointer
ptags = soup.find_all("a", 'item user-tab user-tab-beenthere cursor-pointer ')
lis = ptags[0].find_all('div')
str = lis[0].string
re.sub('<div class="ui label">','',str)
re.sub('</div>','',str)
filew.write('Been there = '+str+'\n')
ptags = soup.find_all("div", 'user-stats_ranking')
lis = ptags[0].find_all('span',{'data-icon' : 'ú'})
str = lis[0].string
#print(str)
filew.write('Foodie level = '+str+'\n')
if(str != '13'):
ptags = soup.find_all("section", 'user-cover ptop ta-center')
lis = ptags[0].find_all('div','ui mini statistics')
lcs = lis[0].find_all('div', {'class':"label"})
str = lcs[0].string
#print(str)
str = str.strip()
filew.write(str+'\n')
else:
filew.write('0 points to level up\n')
linkset = set()
row = soup.find('div', 'row tac user-cover')
lst = row.find('a',{'class':"floating ui blue circular label tooltip_formatted verified-profile"})
if(lst):
# print (lst)
filew.write("Verified User\n")
else:
filew.write('Not a verified user\n')
utag = soup.find('div','ui popup user-common-section user-expertise expertise_popup hidden')
if(utag):
ptags = utag.find_all("li", 'badge')
neighbour = set()
for i in range(0,len(ptags)):
str = ptags[i].get_text()
p = ''
for j in range(0,len(str)):
if str[j] == ' ' and j+1<len(str) and str[j+1] == ' ':
continue
p = p + str[j]
p = p.strip()
#print(p)
neighbour.add(p)
total = repr(len(neighbour)) + ' Neighbourhoods'
filew.write(total+'\n')
for i in neighbour:
filew.write(i+'\n')
else:
filew.write('0 Neighbourhoods\n')
def extract_user_data():
with open ('skip_lines', "r") as myfile:
skip = int(myfile.readline())
with open ('user-links', "r") as myfile:
for skiplines in range(skip):
skiptext = myfile.readline()
for line in myfile.readlines():
line = line.strip()
#print(line)
skip = skip + 1
extract_link(line)
#with open ('user_links', "a") as myfile2:
# for item in userset:
# myfile2.write(item+'\n')
with open ('skip_lines', "w") as myfile1:
myfile1.write(str(skip))
extract_user_data()