-
Notifications
You must be signed in to change notification settings - Fork 13
/
spider_chinese_name.py
97 lines (84 loc) · 3.34 KB
/
spider_chinese_name.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
# -*- coding: utf-8 -*-
# @Time : 2020/2/23 15:43
# @Author : Zhongyi Hua
# @FileName: spider_chinese_name.py
# @Usage: This script is used for spider Chinese name of plants from iplant.com
# @Note:
# @E-mail: [email protected]
import requests
import re
import argparse
from tqdm import tqdm
from pyquery import PyQuery as Pq
from time import sleep
def get_html(url):
my_header = {
'User-Agent': r'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0'}
return requests.get(url, headers=my_header).text
def get_chinese_name(latin_name, retry_num=0):
retry_num += 1
if retry_num > 3:
return
query_url = 'http://www.iplant.cn/info/' + latin_name
try:
a = Pq(get_html(query_url))
try:
chinese_name_inner = re.match('[\u4e00-\u9fa5]+', a('head>title').text()).group(0)
if chinese_name_inner == "植物智":
return
return chinese_name_inner
except AttributeError:
synom_name = a('.infomore .spantxt').text()
if not synom_name == '':
return re.search('[\u4e00-\u9fa5]+', synom_name).group(0) + \
'\t' + \
re.search('[a-zA-Z\s]+', synom_name).group(0).strip()
else:
return
except TimeoutError:
sleep(1)
return get_chinese_name(latin_name, retry_num=retry_num)
def get_latin_name(chinese_name, retry_num=0):
retry_num += 1
if retry_num > 3:
return
query_url = 'http://www.iplant.cn/info/' + chinese_name
try:
a = Pq(get_html(query_url))
latin_name = a('#sptitlel.infolatin').text()
if latin_name == '':
try:
return re.search('[a-zA-Z\s]+', a('.infomore>a').text()).group(0).strip() + \
'\t' + \
re.search('[\u4e00-\u9fa5]+', a('.infomore>a').text()).group(0)
except AttributeError:
return
return latin_name
except TimeoutError:
sleep(1)
get_chinese_name(chinese_name, retry_num=retry_num)
def parse_args():
parser = argparse.ArgumentParser(
description='This is the script for spider Chinese name from iplants website.')
parser.add_argument('-i', '--file_path', required=True,
help='<file_path> A txt file contains one latin name per line')
parser.add_argument('-m', '--model', default='latin', choices=['latin', 'chinese'],
help='<latin|chinese> "latin" for latin to chinese, '
'"chinese" for chinese to latin. DEFAULT:latin')
parser.add_argument('-o', '--result_path', required=True,
help='<file_path> The result file')
args = parser.parse_args()
return args
def main(args):
func = get_chinese_name if args.model == 'latin' else get_latin_name
result_list = []
with open(args.file_path, 'r', encoding='utf-8') as query_file:
query_list = query_file.read().split('\n')
for query_name in tqdm(query_list):
chinese_name = func(query_name)
result_list.append(f'{query_name}\t{chinese_name}')
with open(args.result_path, 'w', encoding='utf-8') as result_file:
result_file.write('\n'.join(result_list))
result_file.close()
if __name__ == '__main__':
main(parse_args())