-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhoscall_fast_query.py
More file actions
executable file
·89 lines (64 loc) · 2.57 KB
/
whoscall_fast_query.py
File metadata and controls
executable file
·89 lines (64 loc) · 2.57 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
#!/usr/bin/env python3
# _*_ coding: utf-8 _*_
from __future__ import print_function
import requests
import sys
from bs4 import BeautifulSoup
API_URL = "https://number.whoscall.com/zh-TW/tw/"
def query_whoscall(phone_number):
""" Query whoscall database by HTTP GET https://whoscall.com/zh-TW/ """
response = requests.get("{}{}".format(API_URL, phone_number))
if 200 == response.status_code:
document = response.content
soup = BeautifulSoup(document, 'html.parser')
title_string = soup.title.string.strip().split(' ')
if len(title_string) == 12:
owner_name = title_string[0]
owner_phone = title_string[2]
owner_city = title_string[6]
elif len(title_string) == 14:
owner_name = "無資料"
owner_phone = title_string[4]
owner_city = title_string[8]
print("\n電話號碼: {}\n擁有者: {}\n擁有者所在城市: {}".format(owner_phone, owner_name, owner_city))
site_container = soup.body.div.contents[3]
site_main_container = site_container.contents[1]
ndp_container = site_main_container.contents[1]
number_info = ndp_container.contents[3]
if number_info.p is None:
pass
elif "這個號碼還沒有被回報" == number_info.p.string.strip():
print("這個號碼還沒有被回報")
else:
owner_name = number_info.h1.string.strip()
number_info_ohours_addr = number_info.contents[5]
all_spans = number_info_ohours_addr.findAll("span")
if all_spans:
business_hour = all_spans[1].span.string
else:
business_hour = None
try:
if all_spans:
address = all_spans[-1].string
else:
address = None
except(AttributeError):
address = u""
if business_hour:
print("營業狀況: {}".format(business_hour))
if address:
print("地址: {}".format(address))
return True
def test_query_whoscall():
""" Test function for whoscall. """
assert query_whoscall("0227208889") is True
assert query_whoscall("0286651720") is True
assert query_whoscall("0286651719") is True
if __name__ == "__main__":
"""
if len(sys.argv) >= 2:
for number in sys.argv[1:]:
query_whoscall(number)
"""
print("Whoscall web page has implemented Google ReCaptcha and cannot be used for command \
line interface now.")