单独文件AttributeE中的Python类

2024-09-29 21:49:54 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我做了一个易受攻击的站点检查器,我用类来做这个。我有一个用于修复代理的类和一个用于检查的类。现在我已经完成了代理类,我开始检查文件,但是我得到了一个错误

代理.py

class proxy:

def __init__(self):
    self.hdr = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.5',
        'Accept-Encoding': 'gzip, deflate',
        'DNT': '1',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1'}

def getrandproxy(self):
    proxies = []
    r = requests.get("https://free-proxy-list.net/anonymous-proxy.html", headers=self.hdr)
    soup = BS(r.text, "lxml")
    container = soup.find("body").section.div.find('div', ['table-responsive']).table.tbody #path from body to proxy table, since parsing directly to it doesn't work
    for tr in container.find_all("tr"):
        ip = tr.td.text
        port = tr.td.next_sibling.text
        anonimity = tr.td.next_sibling.next_sibling.next_sibling.next_sibling.text
        https = tr.td.next_sibling.next_sibling.next_sibling.next_sibling.next_sibling.next_sibling.text
        if https == "yes" and anonimity == "anonymous" or anonimity == "elite proxy":
            server = ip + ":" + port
            proxies.append(server)
    return proxies

注射.py

import requests, proxy
abc = proxy.getrandproxy()
print(abc)

我试着自己打印模块,结果成功了。我只是不明白为什么会出错。我从proxy.py中删掉了一些代码,否则代码会很长。告诉我你是否需要所有的代码。顺便说一下,我剪下的代码不是必需的,它只是一些带有其他函数的代码

错误:

Traceback (most recent call last): File "injection.py", line 2, in <module> abc = proxy.getrandproxy()
AttributeError: module 'proxy' has no attribute 'getrandproxy'

Tags: 代码textpyhttpsself代理findtr

热门问题