Python 爬虫解析案例
学习爬虫时,案例最好能稳定运行。下面这个示例不依赖外部网站,用标准库解析本地 HTML,适合先练“解析数据”的基本流程。
from html.parser import HTMLParser
from http.server import BaseHTTPRequestHandler, HTTPServer
from threading import Thread
from urllib.request import urlopen
class LinkParser(HTMLParser):
def __init__(self):
super().__init__()
self.links = []
def handle_starttag(self, tag, attrs):
if tag == "a":
attrs_dict = dict(attrs)
if "href" in attrs_dict:
self.links.append(attrs_dict["href"])
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
body = b"""
<html>
<body>
<a href="https://example.com/python">Python</a>
<a href="/local">Local</a>
</body>
</html>
"""
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, format, *args):
return
def main():
server = HTTPServer(("127.0.0.1", 0), Handler)
thread = Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
url = f"http://127.0.0.1:{server.server_port}/"
with urlopen(url, timeout=3) as response:
html = response.read().decode("utf-8")
parser = LinkParser()
parser.feed(html)
print(parser.links)
finally:
server.shutdown()
thread.join()
if __name__ == "__main__":
main()