python代码出了什么问题? [重复]

问题描述 投票:0回答:1

Python 代码有什么问题?

import requests
from bs4 import BeautifulSoup
import argparse

class crtShClass():

    def __init__(self,domain):
        self.url = "https://crt.sh/?q=%25."+domain
        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0'}
        self.cookies = {}
        self.foundURLsList = []

    def subdomainScrape(self):
        r = requests.get(self.url,headers=self.headers,timeout=10)
        soup = BeautifulSoup(r.content,'html.parser')

        tableRows = soup.find_all('table')[2].find_all('tr')

        for row in tableRows:
            try:
                subdomain = row.find_all('td')[4].text
                                subdomain = subdomain.replace("*.","")
                                if subdomain not in self.foundURLsList:
                    self.foundURLsList.append(subdomain)
            except Exception as e:
                pass

    def run(self):
        self.subdomainScrape()

    def printSubdomains(self):
        for subdomain in self.foundURLsList:
            print(subdomain)


parser = argparse.ArgumentParser()
parser.add_argument("-d","--domain", help="Domain Name; EX: example.com")
args = parser.parse_args()

crtsh = crtShClass(args.domain)
crtsh.run()
crtsh.printSubdomains()

在 python3.. 终端中,错误是: 22号线 子域名 = subdomain.replace("*.","") TabError:缩进中制表符和空格的使用不一致

enter image description here

在 python2 中..错误是:

回溯(最近一次调用最后一次): 文件“certsh.py”,第 2 行,位于 从 bs4 导入 BeautifulSoup 导入错误:没有名为 bs4 的模块

enter image description here

python security
1个回答
0
投票

只需尝试纠正缩进,如下所示:

....
try:
    subdomain = row.find_all('td')[4].text
    subdomain = subdomain.replace("*.","")
    if subdomain not in self.foundURLsList:
        self.foundURLsList.append(subdomain)
except Exception as e:
    pass
...

当前版本的bs4不支持python 2

Beautiful Soup 对 Python 2 的支持已于 12 月 31 日停止, 2020 年:Python 2 本身的日落日期一年后。由此 今后,新的 Beautiful Soup 开发将专门针对 Python 3.Beautiful Soup 4 最终版本支持 Python 2 是 4.9.3.

© www.soinside.com 2019 - 2024. All rights reserved.