-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
74 lines (59 loc) · 2.01 KB
/
Copy pathutils.py
File metadata and controls
74 lines (59 loc) · 2.01 KB
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
#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# @Author :triangle
# @Time :2019/4/30 14:04
# @Filename :utils.py
import asyncio
import aiohttp
from settings import HEADERS, REQUEST_TIMEOUT, REQUEST_DELAY
LOOP = asyncio.get_event_loop()
async def _get_page(url, sleep):
"""
获取并返回网页内容
"""
async with aiohttp.ClientSession() as session:
try:
await asyncio.sleep(sleep)
async with session.get(
url, headers=HEADERS, timeout=REQUEST_TIMEOUT
) as resp:
return await resp.text()
except:
return ""
async def _get_other_page(url, sleep):
"""
站大爷代理专用,防止跳转
"""
async with aiohttp.ClientSession() as session:
try:
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Host': 'ip.zdaye.com',
'Referer': 'http://ip.zdaye.com/dayProxy.html',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
}
await asyncio.sleep(sleep)
async with session.get(
url, headers=headers, timeout=REQUEST_TIMEOUT
) as resp:
return await resp.text()
except:
return ""
def requests(url, sleep=REQUEST_DELAY):
"""
请求方法,用于获取网页内容
:param url: 请求链接
:param sleep: 延迟时间(秒)
"""
html = LOOP.run_until_complete(asyncio.gather(_get_page(url, sleep)))
if html:
return "".join(html)
def requests_other(url, sleep=REQUEST_DELAY):
"""
请求方法,用于获取网页内容
:param url: 请求链接
:param sleep: 延迟时间(秒)
"""
html = LOOP.run_until_complete(asyncio.gather(_get_other_page(url, sleep)))
if html:
return "".join(html)