forked from Guovin/iptv-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
51 lines (43 loc) · 1.22 KB
/
utils.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
import requests
import re
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
headers = {
"Accept": "*/*",
"Connection": "keep-alive",
"Accept-Language": "zh-CN,zh;q=0.8",
"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",
}
session = requests.Session()
def get_source_requests(url, data=None, proxy=None, timeout=30):
"""
Get the source by requests
"""
proxies = {"http": proxy}
ua = UserAgent()
headers["User-Agent"] = ua.random
if data:
response = session.post(
url, headers=headers, data=data, proxies=proxies, timeout=timeout
)
else:
response = session.get(url, headers=headers, proxies=proxies, timeout=timeout)
source = re.sub(
r"<!--.*?-->",
"",
response.text,
flags=re.DOTALL,
)
return source
def get_soup_requests(url, data=None, proxy=None, timeout=30):
"""
Get the soup by requests
"""
source = get_source_requests(url, data, proxy, timeout)
soup = BeautifulSoup(source, "html.parser")
return soup
def close_session():
"""
Close the requests session
"""
session.close()