#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

posted on 2024-10-24 22:14  我爱你的  阅读(2)  评论(0编辑  收藏  举报