-
Notifications
You must be signed in to change notification settings - Fork 3
/
dy.py
138 lines (123 loc) · 4.1 KB
/
dy.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
# coding: utf-8
from __future__ import print_function
from pprint import pprint
import re
import requests
import w3lib
from parsel import Selector
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
try:
from urllib.parse import urljoin
except ImportError:
from six.moves.urllib.parse import urljoin
def download(url, proxy=None, rain_num=2):
print("dowing", url)
heads = {
'Accept': 'text/*, application/xml',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Mobile Safari/537.36',
"X-Requested-With": "XMLHttpRequest",
"Host": "www.douyin.com",
"Upgrade-Insecure-Requests": "1"
}
try:
html = requests.get(url, headers=heads).text
except Exception as e:
print("Downing error", e.reason)
html = None
if rain_num > 0:
if hasattr(e, 'code') and 500 <= e.code < 600:
return download(url, rain_num - 1)
return html
def spider(uid):
url = "https://www.douyin.com/share/user/%s" % uid
body = download(url)
xbody = Selector(text=body)
item = dict()
nick_name = xbody.xpath("//p[@class='nickname']/text()").extract_first()
print(nick_name)
works = xbody.xpath("//div[@class='user-tab active tab get-list']/span").extract_first()
works = re.findall('>([\s\S]+?)<', works)
works = jiexi(works).strip()
like_num = xbody.xpath("//div[@class='like-tab tab get-list']/span").extract_first()
like_num = re.findall('>([\s\S]+?)<', like_num)
like_num = jiexi(like_num).strip()
douyin_id = xbody.xpath("//p[@class='shortid']").extract_first()
douyin_id = re.findall('>([\s\S]+?)<', douyin_id)
douyin_id = jiexi(douyin_id).replace(u"抖音ID:", '').strip()
try:
info = xbody.xpath("//span[@class='info']/text()").extract_first().strip()
except Exception as e:
info = ''
guanzhu = xbody.xpath("//span[contains(@class,'focus block')]/span[@class='num']").extract_first()
guanzhu = re.findall('>([\s\S]+?)<', guanzhu)
guanzhu = jiexi(guanzhu)
fans = xbody.xpath("//span[contains(@class,'follower block')]/span[@class='num']").extract_first()
fans = re.findall('>([\s\S]+?)<', fans)
fans = jiexi(fans)
zan = xbody.xpath("//span[contains(@class,'liked-num block')]/span[@class='num']").extract_first()
zan = re.findall('>([\s\S]+?)<', zan)
zan = jiexi(zan)
item['douyin_id'] = douyin_id
item['nick_name'] = nick_name
item["fans"] = fans
item["zan"] = zan
item["guanzhu"] = guanzhu
item['works'] = works
item['like_num'] = like_num
item['info'] = info
pprint(item)
def jiexi(lists):
pat = {
u"\ue60d": 0,
u"\ue603": 0,
u"\ue616": 0,
u"\ue60e": 1,
u"\ue618": 1,
u"\ue602": 1,
u"\ue605": 2,
u"\ue610": 2,
u"\ue617": 2,
u"\ue611": 3,
u"\ue604": 3,
u"\ue61a": 3,
u"\ue606": 4,
u"\ue619": 4,
u"\ue60c": 4,
u"\ue60f": 5,
u"\ue607": 5,
u"\ue61b": 5,
u"\ue61f": 6,
u"\ue612": 6,
u"\ue608": 6,
u"\ue61c": 7,
u"\ue60a": 7,
u"\ue613": 7,
u"\ue60b": 8,
u"\ue61d": 8,
u"\ue614": 8,
u"\ue615": 9,
u"\ue61e": 9,
u"\ue609": 9,
"w": "w",
".": "."
}
_li = list()
for i in lists:
if str(i).strip():
i = i.replace(u'<i class="icon iconfont follow-num">', "").strip()
i = i.replace(u'<i class="icon iconfont ">', "").strip()
i = i.replace(u'<i class="icon iconfont tab-num">', "").strip()
i = pat.get(i, i)
_li.append(str(i))
return "".join(_li)
if __name__ == '__main__':
uids = [
"57720812347", "93046013277", "72096309936", "60637177764", "69914084602", "72722865756", "58486060366", "95433824498",
"77267568314", "52616983119", "61141281259", "58900737309"
]
for uid in uids:
spider(uid)