如何使用Python查找特定文本并返回打印?

时间:2017-06-08 17:33:04

标签: python beautifulsoup

所以我想用beautifulsoup来搜索: 服务器:Apache / 2.4.10(Unix) 您可以在https://www.shodan.io/host/46.173.206.195上找到该文字 我想要打印:发现后,打印' Apache发现''或者没有找到Apache#'' 我该怎么做呢? 我的代码是:

from bs4 import BeautifulSoup
import requests
import sys
import re
import urllib2
from colorama import init
from colorama import Fore, Back, Style
init()
s = requests.Session()
print(Fore.GREEN + 'Made by Zaseth.')
print(Fore.GREEN + "[*] If you get the following error: TypeError: 'NoneType' object has no attribute '__getitem__'")
print(Fore.GREEN + "[*] This error means that your target is not on Shodan.")
print(Style.RESET_ALL)
shodanURL = raw_input(Fore.GREEN + ("Your target IP: "))

r = s.get('https://www.shodan.io/host/' + shodanURL)
r.status_code = ('Shodan Status: ' + str(r.status_code))

soup = BeautifulSoup(r.text, "html.parser")

openPorts = soup.find("meta", attrs={"name": "twitter:description"})["content"]
print(Fore.GREEN + (openPorts))

serviceDetails = iter(soup.findAll("div", attrs={"class": "service-details"}))
serviceMains = soup.findAll("div", attrs={"class": "service-main"})

for serviceMain in serviceMains:
    currentService = next(serviceDetails)

    print(Fore.GREEN + ("Port: " + currentService.find("div", attrs={"class": "port"}).contents[0]))
    print(Fore.GREEN + ("Protocol: " + currentService.find("div", attrs={"class": "protocol"}).contents[0]))
    print(Fore.GREEN + ("State: " + currentService.find("div", attrs={"class": "state"}).contents[0]))
    print(Fore.GREEN + ("------"))

    serviceInfo = serviceMain.find("pre").contents[0]
    print(Fore.GREEN + (serviceInfo))
    print(Fore.GREEN + '[*] Header Done.')
    print(Fore.GREEN + '[*] SSL info response can sometimes be limited.')
    print(Style.RESET_ALL)

我无法在这里输入它,因为它会搞砸了。

1 个答案:

答案 0 :(得分:0)

您是否知道有一个官方的Shodan API允许免费的基于IP的查找?并且有一个Python library for Shodan可以很容易地与它进行交互。刮网站违反了服务条款,会让您长期受阻。以下是您尝试实现的更简单的版本:

from shodan import Shodan

api = Shodan("API KEY") # Visit https://account.shodan.io for your API key
host = api.host('46.173.206.195')

# The top-level "ports" property has a list of all the open ports
print('Open Ports: {}'.format(host['ports']))

# The "data" property contains a list of all the banners
for banner in host['data']:
    print('Port: {}'.format(banner['port']))
    print('Protocol: {}'.format(banner['transport']))
    print(banner['data'])