-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnw.records.py
139 lines (128 loc) · 4.81 KB
/
cnw.records.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
import os
import re
from operator import itemgetter
from city import getArea
def getChinese(context):
#context = context.decode("utf-8") # convert context from str to unicode
filtrate = re.compile(u'[^\u4E00-\u9FA5]') # non-Chinese unicode range
context = filtrate.sub(r'', context) # remove all non-Chinese characters
#context = context.encode("utf-8") # convert unicode back to str
return context
def getDigit(context):
#context = context.decode("utf-8") # convert context from str to unicode
filtrate = re.compile(u'[^\d+]') # non-Chinese unicode range
context = filtrate.sub(r'', context) # remove all non-Chinese characters
#context = context.encode("utf-8") # convert unicode back to str
return context
database=open("database/cnw.records.txt","w")
records=[]
file=open('urls/cn.woman.txt', 'r')
lines=file.readlines()
count = 0
# Strips the newline character
for line in lines:
count += 1
print(count)
words=str(line).split('|')
print(words[0])
print(words[1])
print(words[2])
print(words[3])
index=words[0]
name=words[1]
url=words[2]
imgUrl=words[3]
detailUrl="profiles/cnw/"+index+name+"/"+index+name+".txt"
if not os.path.exists(detailUrl):
continue
pfile = open(detailUrl, 'r')
dLines=pfile.readlines()
for dline in dLines:
print(dline)
if "中文名" in dline.split(":")[0].replace("\n",""):
name = dline.split(":")[1].replace("\n", "").strip()
name = name.split("[")[0].strip()
if "浏览次数" in dline.split(":")[0].replace("\n",""):
visit=dline.split(":")[1].replace("\n","").strip()
if "出生日期" in dline.split(":")[0].replace("\n",""):
year=re.search(":(.+?)年",dline)
if year:
year=year.group(1)
else:
year="*"
month=re.search("年(.+?)月",dline)
if month:
month=month.group(1)
else:
month = re.search(":(.+?)月", dline)
if month:
month=month.group(1)
else:
month="*"
day=re.search("月(.+?)日",dline)
if day:
day=day.group(1)
else:
day="*"
year=getDigit(year)
month=getDigit(month)
day=getDigit(day)
if "职业" in dline.split(":")[0].replace("\n",""):
job=dline.split(":")[1].replace("\n","")
job=job.split("、")[0]
job=getChinese(job)
if "运动项目" in dline.split(":")[0].replace("\n",""):
job="运动员"
if "国籍" in dline.split(":")[0].replace("\n",""):
country=dline.split(":")[1].replace("\n","")
country=getChinese(country)
if "出生地" in dline.split(":")[0].replace("\n",""):
# province=dline.split(":")[1].replace("\n","")
# province=re.search(":(.+?)省",dline)
# if province:
# province=province.group(1)
# else:
# province="*"
# city = dline.split(":")[1].replace("\n", "")
# city = re.search("省(.+?)市", dline)
# if city:
# city = city.group(1)
# else:
# city = re.search(":(.+?)市", dline)
# if city:
# city=city.group(1)
# else:
# city=dline.split(":")[1].replace("\n", "")
# if "香港" in dline:
# province="香港"
# city="香港"
# if "澳门" in dline:
# province = "澳门"
# city = "澳门"
# if "台湾" in dline:
# province="台湾"
# city="台湾"
province,city=getArea(dline.split(":")[1].replace("\n", ""))
record = index + "|"+visit +"|"+ name + "|" + "M"+"|"+year+"|"+month+"|"+day+"|"+job+"|"+country+"|"+province+"|"+city+"|"+imgUrl
print(record)
if visit and int(visit)>=1000000:
tuple = (index, int(visit), name, "F", year, month, day, job, country, province, city, imgUrl)
records.append(tuple)
year = ""
month = ""
day = ""
country = ""
province = ""
city = ""
job = ""
srecords=sorted(records,key=itemgetter(1),reverse=True)
# database.write("const database=[")
previous=""
for tuple in srecords:
record=tuple[0]+"|"+str(tuple[1])+"|"+tuple[2]+"|"+tuple[3]+"|"+tuple[4]+"|"+tuple[5]+"|"+tuple[6]+"|"+tuple[7]+"|"+tuple[8]+"|"+tuple[9]+"|"+tuple[10]+"|"+tuple[11].replace("\n","")
if previous!=tuple[2]+tuple[3]+tuple[4]:
previous = tuple[2]+tuple[3]+tuple[4]
# database.write("\n"+"'"+record+"'"+",")
database.write(record + "\n")
print(record)
# database.write("]")