Tools_py

我爱你的發表於2024-10-24
#Tools.py
import re
import subprocess
from functools import partial

import requests
from parsel import Selector

subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
import execjs



def zip_():
name_list = ['黎明','大軍']
price_list =[11,22]
for name, price in zip(name_list, price_list):
item = {}
item['name'] = name
item['price'] = price
print(item)
class TOOLS:

@classmethod
def get_html_data(cls, text, path, method="xpath"):
"""
:param text: html頁面資料
:param method: 預設填的是xpath,隨便其他的就是css
:return: 返回的是list
"""
sel = Selector(text)
if method == 'xpath':
data = sel.xpath(path).getall() # xpath提取
return data
else:
data = sel.css(path).getall() # css提取
return data

@classmethod
def get_proxies_list(cls, num):
"""
:param num: ip_list的數量
:return: 返回的是ip_list ['http': f'http://163'...]
"""
res = requests.get(url=f"http://api.89ip.cn/tqdl.html?api=1&num={num * 2}&port=&address=&isp=").text

ip_list = re.compile("<br>(.*?)<br>").findall(res)

proxies_list = []
for i in ip_list:
# ip = {
# "http": i
# }
ip = {
'http': f'http://{i}',
# 'https': f'https://{i}'
}
proxies_list.append(ip)
# print(len(proxies_list))
return proxies_list

@classmethod
def get_js_data(cls, js_path, function_name, *args):
js_code = open(js_path, encoding='utf-8').read()
ctx = execjs.compile(js_code)

# 第一個引數為ja程式碼中的函式名, 後面為函式對應的引數
result = ctx.call(function_name, *args)
return result
# print(result)
@classmethod
def re_method(cls,re_method,data):

data = re.compile(re_method).findall(data)
return data