0

我必须通过大量的 ip 搜索特定的 url。我在python中编写了一个脚本,检查端口是否打开,然后使用httplib检查url是否存在,它工作得很好!我的问题是我得到了太多的误报,因为一些网络设备在请求我的页面时给出状态 200,并在正文上返回一个带有 400 错误的页面

这是我的代码:

def MyPage(self,ip):
    try:
        conn = httplib.HTTPConnection(ip)
        conn.request("HEAD", "/path/to/mypage.php")
        resp = conn.getresponse()
        if (resp.status == 200):
            return True
        else :
            return False
    except :
        return False
4

1 回答 1

0

我解决了检查页面正文上的标题标签的问题

def Mypage(self,ip):
    try:
        conn = httplib.HTTPConnection(ip)
        conn.request("GET", "/path/to/mypage.php")
        resp = conn.getresponse()
        if (resp.status == 200):
            html = BeautifulSoup(resp.read())
            data = html.find('title')
            titulo = str(data.contents[0])
            if titulo == "THE TITLE":
                return True
            else:
                return False
        else :
            return False
    except :
        return False
于 2013-07-12T01:21:35.337 回答