-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_ops.py
199 lines (181 loc) · 10.9 KB
/
web_ops.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import requests
from bs4 import BeautifulSoup
import re
import random
import time
def decode_cite_text(text):
author_pattern = re.compile(r'author\s*=\s*\{(.*?)\}', re.DOTALL)
match = author_pattern.search(text)
authors = []
if match:
authors = match.group(1).split(' and ')
authors = [author.strip().replace(' ','').replace(',',' ') for author in authors]
return authors
def get_random_ua():
user_agents = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.54',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.2 Safari/605.1.15',
# 添加更多的用户代理,
]
return random.choice(user_agents)
def get_random_cookie():
cookies = [
"__cfduid=d25efa21dde4a114e7dd96b477e0aba381531100939; FR24ID=0l69ten5b83uoqjui6ufipkbb14hb2li3pd8hcfqtar0uptt84r0; __utma=177865842.2011292994.1531100944.1531100944.1531100944.1; __utmc=177865842; __utmz=177865842.1531100944.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __utmt=1; __utmb=177865842.1.10.1531100944",
"__Secure-ENID=21.SE=AahQx5gYvJ-uxVfRpFaLXAi30qm23F3UVYSYaVHf2dhSeAA42kOONEoajw6hlPGetOS-PXHKSwCx3dwJls0S7vLQTE47CjYEs3p_SOmxbR_4rod80TkGV8HXWyvSR2ye4t3T4b_tMZURHU1zvcgrjlM9ZHjDPmrwbNxvRCQ7g5vA2bIyiFlV2mfIAwPaQfc9RwMYf2AsWgQ8n71QEAnYfzTCNE9144tafZwfLa8eIafyhXfeK7WnoiKp9acpzoxLGwTdSrFm2urUthRXp0EWJooc9kE2Jfoj0Ecjm20; HSID=AEmGbzz4ZvrmDfvnU; SSID=Atrpx2xqPy1sbd23O; APISID=585xvnk1N5J95HbV/Ac9xiINqoXJptMa85; SAPISID=RKT6gZxPQybCIhyV/AGA2VMrM1eNAdKTxp; __Secure-1PAPISID=RKT6gZxPQybCIhyV/AGA2VMrM1eNAdKTxp; __Secure-3PAPISID=RKT6gZxPQybCIhyV/AGA2VMrM1eNAdKTxp; GSP=A=KhlVtQ:CPTS=1732600504:LM=1732600504:S=o5pGDcQSM1TCFVC8; SID=g.a000rwiClXYbQLfoAI4PS0lXVqKoBT5NX3MqEajXtcP_bF1LjzASly_nDKBf1YUcCE0QigknGAACgYKAbkSARESFQHGX2Mi5TX6r0WWWf3EX9Z6Ku78mRoVAUF8yKqXU1TTnDuZzRmd7mvGCoLE0076; __Secure-1PSID=g.a000rwiClXYbQLfoAI4PS0lXVqKoBT5NX3MqEajXtcP_bF1LjzASjz77ReXb4rwvz6Sf1QS5WgACgYKAVQSARESFQHGX2MipDJ0Q8El9Bo1ZVPUanGeMhoVAUF8yKpj1sF7MedsYCsWKbalHHSG0076; __Secure-3PSID=g.a000rwiClXYbQLfoAI4PS0lXVqKoBT5NX3MqEajXtcP_bF1LjzASElRIm5qtI1Fm23QrYowHcgACgYKAdISARESFQHGX2MiNImDRPQDhuTDvnqdG3zPNxoVAUF8yKqohZnrp6GtEIYk6cfWLiLS0076; SEARCH_SAMESITE=CgQIgZ0B; AEC=AZ6Zc-VoPElF8Q8p3o7M5xHNX2TweC5SKWn73RHXEEOPFnWaUJEgN3makA; __Secure-1PSIDTS=sidts-CjIBmiPuTfNCC7sODJGt0tIAUTWmNT1g6qz3ArW4EhUkI2Z6BGxkSdtMzPQObCnwE1498hAA; __Secure-3PSIDTS=sidts-CjIBmiPuTfNCC7sODJGt0tIAUTWmNT1g6qz3ArW4EhUkI2Z6BGxkSdtMzPQObCnwE1498hAA; NID=520=DYXabZX9E0Owkf8qWSZReqfSfmmLd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用户代理,
]
return random.choice(cookies)
def get_authors(paper_id,page,i,is_debug=False):
if paper_id == '':
return []
headers = {
"User-Agent": get_random_ua(),
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"accept-language": "zh-CN,zh;q=0.9",
"accept-encoding": "gzip, deflate, br, zstd",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"cache-control": "max-age=0",
"cookie": get_random_cookie()
}
if page == 0:
id = f'{i}'
else:
id = f'{page}{i}'
url = f'https://scholar.google.com/scholar?q=info:{paper_id}:scholar.google.com/&output=cite&scirp={id}&hl=en'
try:
# time.sleep(random.randint(1, 5))
res = requests.get(url, headers=headers, timeout=10)
if is_debug:
with open('./debug.log','a') as f:
f.write(str(res.status_code)+'\n')
f.write(str(res.text)+'\n')
f.write('\n')
if res.status_code == 200:
soup = BeautifulSoup(res.text, 'html.parser')
bibtex = soup.find('a', string='BibTeX')
if bibtex:
bibtex_link = bibtex['href']
res = requests.get(bibtex_link, headers=headers, timeout=10)
if res.status_code == 200:
if is_debug:
with open('./debug.log','a') as f:
f.write(str(res.status_code)+'\n')
f.write(str(res.text)+'\n')
f.write('\n')
aths = decode_cite_text(res.text)
if is_debug:
with open('./debug.log','a') as f:
f.write(';'.join(aths)+'\n')
f.write('\n')
return aths
else:
return []
except :
pass
return []
def get_authors_proxy(paper_id,is_debug=False):
if paper_id == '':
return []
headers = {
"User-Agent": get_random_ua(),
"accept-language": "zh-CN,zh;q=0.9",
"accept-encoding": "gzip, deflate, br, zstd",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"cache-control": "max-age=0",
"cookie": get_random_cookie()
}
url = f'https://scholar.google.com/scholar?q=info:{paper_id}:scholar.google.com/&output=cite&scirp=0&hl=en'
try:
proxyUrl = "http://%(user)s:%(password)s:A%(area)d@%(server)s" % {
"user": 'J2P4MW5D',
"password": '4DF1563E29B1',
"server": 'overseas-us.tunnel.qg.net:19862',
"area": 990100,
}
proxies = {
"http": proxyUrl,
"https": proxyUrl,
}
res = requests.get(url, headers=headers, proxies=proxies,timeout=10)
if is_debug:
with open('./debug.log','a') as f:
f.write(str(res.status_code)+'\n')
f.write(str(res.text)+'\n')
f.write('\n')
if res.status_code == 200:
soup = BeautifulSoup(res.text, 'html.parser')
bibtex_link = soup.find('a', string='BibTeX')['href']
res = requests.get(bibtex_link, headers=headers, proxies=proxies, timeout=10)
if res.status_code == 200:
if is_debug:
with open('./debug.log','a') as f:
# f.write(str(res.status_code)+'\n')
f.write(str(res.text)+'\n')
f.write('\n')
aths = decode_cite_text(res.text)
if is_debug:
with open('./debug.log','a') as f:
f.write(';'.join(aths)+'\n')
f.write('\n')
return aths
else:
return []
else:
return []
except Exception as e:
pass
return []
def safe_bot_request_proxy(url,is_debug=False):
headers = {
"User-Agent": get_random_ua(),
"accept-language": "zh-CN,zh;q=0.9",
"accept-encoding": "gzip, deflate, br, zstd",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"cache-control": "max-age=0",
"cookie": get_random_cookie()
}
# proxyUrl = "http://%(user)s:%(password)s:A%(area)d@%(server)s" % {
# "user": 'J2P4MW5D',
# "password": '4DF1563E29B1',
# "server": 'overseas-us.tunnel.qg.net:19862',
# "area": 990100,
# }
proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
"user": 'J2P4MW5D',
"password": '4DF1563E29B1',
"server": 'overseas-us.tunnel.qg.net:19862',
}
proxies = {
"http": proxyUrl,
"https": proxyUrl,
}
bot_count = 0
while 1:
res = requests.get(url, headers=headers, proxies=proxies,timeout=10)
if is_debug:
with open('debug.log','a') as f:
f.write(str(res.status_code)+'\n')
f.write(str(res.text)+'\n')
f.write('\n')
if res.status_code == 200:
soup = BeautifulSoup(res.text, "html.parser")
bot_check = soup.find_all("div", id="gs_captcha_ccl")
if len(bot_check) == 0:
return res
bot_count+=1
# waitTime = random.randint(2,5)*10
# print(f'find bot verify , wait for {waitTime} s')
# time.sleep(waitTime)
# if bot_count > 10:
# print("Error with loading web page")
# raise ValueError("Webpage Error, for most reason maybe banned by google!")