-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
262 lines (177 loc) · 6.67 KB
/
test.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# import asyncio
# import functools
# import time
# import random
# import multiprocessing
# import aiomultiprocess
# import uvloop
# import numpy as np
# from tqdm import tqdm
# # # 下载协程
# # async def download(url):
# # cost = time.time()
# # await asyncio.sleep(3) # 模拟1秒的下载过程
# # return 'cost: {}'.format(round(time.time()-cost, 3))
# # # 回调函数
# # def on_finish(task):
# # print('下载完成:', task.result()) # 获取协程返回值或者抛出的异常
# # def mp_schedule():
# # pages = list(range(500))
# # prefix = 'https://yuerblog.cc/'
# # urls = ['{}{}'.format(prefix, page) for page in pages]
# # # url_chunks = np.array_split(urls ,5)
# # with multiprocessing.Pool(processes=5) as pool:
# # for url_chunk in urls:
# # pool.apply_async(download, args = (url_chunk, ), callback = on_finish)
# # async def aio_mp_schedule():
# # pages = list(range(500))
# # prefix = 'https://yuerblog.cc/'
# # urls = ['{}{}'.format(prefix, page) for page in pages]
# # pbar = tqdm(total=len(urls), ncols=80)
# # async with aiomultiprocess.Pool(processes=5, loop_initializer=uvloop.new_event_loop) as pool:
# # async for result in pool.map(download, urls):
# # pbar.update()
# # async def schedule():
# # pages = list(range(500))
# # prefix = 'https://yuerblog.cc/'
# # tasks = [asyncio.create_task(download('{}{}'.format(prefix, page))) for page in pages]
# # responses = [await t for t in tqdm(asyncio.as_completed(tasks), total=len(tasks), ncols=80)]
# # if __name__ == '__main__':
# # uvloop.install()
# # cost = time.time()
# # # asyncio.run(schedule())
# # mp_schedule()
# # print("total time cost: {}".format(round(time.time()-cost, 3)))
# import multiprocessing as mp
# import time
# async def foo_pool(x):
# await asyncio.sleep(2)
# return x*x
# result_list = []
# def log_result(result):
# # This is called whenever foo_pool(i) returns a result.
# # result_list is modified only by the main process, not the pool workers.
# result_list.append(result)
# print(result)
# def apply_async_with_callback():
# pool = mp.Pool(processes=3)
# for i in range(10):
# pool.apply_async(foo_pool, args = (i, ), callback = log_result)
# pool.close()
# pool.join()
# print(result_list)
# if __name__ == '__main__':
# cost = time.time()
# apply_async_with_callback()
# print("total cost: {}".format(round(time.time()-cost, 3)))
# # def on_finish(future, n):
# # print('{}: future done: {}'.format(n, future.result()))
# # async def register_callbacks(all_done):
# # print('registering callbacks on future')
# # page = 1
# # while True:
# # all_done.add_done_callback(functools.partial(on_finish, n=page))
# # page += 1
# # if page > 5:
# # return
# # async def main(all_done):
# # await register_callbacks(all_done)
# # print('setting result of future')
# # all_done.set_result('the result')
# # event_loop = asyncio.get_event_loop()
# # try:
# # all_done = asyncio.Future()
# # event_loop.run_until_complete(main(all_done))
# # finally:
# # event_loop.close()
# # async def download(fut, delay, value):
# # cost = time.time()
# # # Sleep for *delay* seconds.
# # await asyncio.sleep(delay)
# # # Set *value* as a result of *fut* Future.
# # fut.set_result('task: {} cost: {}'.format(value, round(time.time()-cost, 3)))
# # async def main():
# # # Get the current event loop.
# # # loop = asyncio.get_running_loop()
# # # Create a new Future object.
# # # fut = loop.create_future()
# # # Run "set_after()" coroutine in a parallel Task.
# # # We are using the low-level "loop.create_task()" API here because
# # # we already have a reference to the event loop at hand.
# # # Otherwise we could have just used "asyncio.create_task()".
# # while True:
# # page = 1
# # asyncio.create_task(download(fut, 1, page))
# # if page > 5:
# # break
# # # Wait until *fut* has a result (1 second) and print it.
# # print(await fut)
# # asyncio.run(main())
# import aiohttp
# import asyncio
# import time
# from bs4 import BeautifulSoup
# from urllib.request import urljoin
# import re
# import multiprocessing as mp
# base_url = "https://morvanzhou.github.io/"
# seen = set()
# unseen = set([base_url])
# def parse(html):
# soup = BeautifulSoup(html, 'lxml')
# urls = soup.find_all('a', {"href": re.compile('^/.+?/$')})
# title = soup.find('h1').get_text().strip()
# page_urls = set([urljoin(base_url, url['href']) for url in urls])
# url = soup.find('meta', {'property': "og:url"})['content']
# return title, page_urls, url
# async def crawl(url, session):
# r = await session.get(url)
# html = await r.text()
# await asyncio.sleep(0.1) # slightly delay for downloading
# return html
# async def main(loop):
# processes = 8
# pool = mp.Pool(processes) # slightly affected
# async with aiohttp.ClientSession() as session:
# count = 1
# while len(unseen) != 0:
# print('\nAsync Crawling...')
# tasks = [loop.create_task(crawl(url, session)) for url in unseen]
# finished, unfinished = await asyncio.wait(tasks)
# htmls = [f.result() for f in finished]
# print('\nDistributed Parsing...')
# parse_jobs = [pool.apply_async(parse, args=(html,)) for html in htmls]
# results = pool.map(parse, htmls)
# # results = pool.map_async(parse, htmls).get()
# # print(parse_jobs.get())
# results = [j.get() for j in parse_jobs]
# # print(results)
# print('\nAnalysing...')
# seen.update(unseen)
# unseen.clear()
# for title, page_urls, url in results:
# # print(count, title, url)
# unseen.update(page_urls - seen)
# count += 1
# if __name__ == "__main__":
# t1 = time.time()
# loop = asyncio.get_event_loop()
# loop.run_until_complete(main(loop))
# loop.close()
# print("Async total time: ", time.time() - t1)
import requests
import json
import pandas as pd
url = 'http://127.0.0.1:5010/get_all/'
def a():
try:
resp = requests.get(url)
except Exception as e:
print(f'url: {url}, error: {e}')
return pd.DataFrame()
proxy_list_string = json.loads(resp.content)
for proxy in proxy_list_string:
print(proxy)
# proxy_dict = json.loads(proxy)
# print(proxy_dict)
a()