liumiaomiao 1 年之前
父节点
当前提交
01baa38089
共有 1 个文件被更改,包括 15 次插入4 次删除
  1. 15 4
      lib/linkapi.py

+ 15 - 4
lib/linkapi.py

@@ -4,10 +4,20 @@ from PIL import Image, ImageChops
 from bs4 import BeautifulSoup
 import requests
 class APILink():
+    headers = {
+        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67;jy-test"
+    }
+    #headers设置为全局变量
+    GSTORE['headers'] = headers
+    # session对象设置为全局变量
+    s = requests.Session()
+    GSTORE['s'] = s
+
     #打开链接,返回title
     def obtain_url_title(self,url):
-        response = requests.get(url,headers = {
-        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36;jy-test'})
+        headers=GSTORE['headers']
+        s = GSTORE['s']
+        response = s.get(url,headers=headers)
         response.encoding = 'utf-8'  # 设置编码为utf-8
         soup = BeautifulSoup(response.text, 'html.parser')
         title = soup.title.string
@@ -15,8 +25,9 @@ class APILink():
 
     #打开链接,status=200,返回true
     def open_url_status(self,url):
-        response = requests.get(url,headers = {
-        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36;jy-test'})
+        headers=GSTORE['headers']
+        s = GSTORE['s']
+        response = s.get(url,headers=headers)
         status_code = response.status_code
         if status_code==200:
             return True