首页 > 解决方案 > 如何在 urlopen 响应中搜索特定字符串?

问题描述

我想查看 Troy Hunt 的网站“ https://haveibeenpwned.com/Passwords ”,如果他发布了新的密码文件,则使用 Python 进行自动化。为此,我阅读了该网站并想在其中搜索一个字符串以获取该文件的当前版本。这些总是以模式 ....v5.7z 命名。v 在这里代表版本。

# -*- coding: utf-8 -*-

import os
import urllib2
#from urllib2 import Request

from urllib2 import Request, urlopen, URLError, HTTPError
someurl='https://haveibeenpwned.com/Passwords'
req = Request(someurl, headers={'User-Agent': 'Mozilla/5.0'})
try:
    response = urlopen(req)
except HTTPError as e:
    print 'The server couldn\'t fulfill the request.'
    print 'Error code: ', e.code
except URLError as e:
    print 'We failed to reach a server.'
    print 'Reason: ', e.reason
else:
    print  "everything is fine"
    response = urllib2.urlopen(req)
    the_page = response.read()
    print(the_page)


在 "the_page" 中是页面的整个 HTML 代码。我该如何搜索它?

我不允许使用 beautifulsoap 或解析器..

标签: pythonhtmlparsingrequesturllib2

解决方案


推荐阅读