-
Notifications
You must be signed in to change notification settings - Fork 3
/
divar.py
46 lines (40 loc) · 1.71 KB
/
divar.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
import requests as req
from bs4 import BeautifulSoup as BS
import time
mainUrl = "https://divar.ir/s"
city = (input("specify city (example: 'tehran' or 'mashhad' ...) : ")).lower()
# city = "tehran"
category= (input("specify category (example: 'website-services' or 'mobile-phones' or 'it-computer-jobs' ...) : ")).lower()
# category= "website-services"
referer = "%s/%s/%s"%(mainUrl, city, category)
# referer = "https://divar.ir/s/tehran/website-services"
startPage = int(input("Start page: "))
lastPage = ""
for i in range(startPage, 100):
phones = []
url = "https://divar.ir/s/%s/%s?page=%d"%(city, category, i)
reqToGetTokens = req.get(url)
if "post-card" not in reqToGetTokens.text:
break
contentAsBS = BS(reqToGetTokens.text, 'html.parser')
post_cards = contentAsBS.select(".post-card-item .kt-post-card")
print("#### Page %d"%i)
for j in range(len(post_cards)):
reqToGetPhone = req.get("https://api.divar.ir/v5/posts/%s/"%post_cards[j]['href'].split("/")[-1])
if reqToGetPhone.ok == False:
lastPage = "Error at page %d item %d"%(i, j + 1)
break
if '09' in reqToGetPhone.json()['widgets']['contact']['phone']:
phones.append(reqToGetPhone.json()['widgets']['contact']['phone'])
else:
phones.append("only chat")
print("### token -> %s"%post_cards[j]['href'].split("/")[-1])
print("### phone -> %s"%phones[j])
# tokens.append(post_cards[j]['href'].split("/")[-1])
time.sleep(3)
if lastPage != "":
break
with open('divar-%s-%s.txt'%(city, category), 'a') as f:
for phone in phones:
f.write("%s\n"%phone)
if lastPage: print(lastPage)