0

现在我想从网页中抓取keywords元数据,如下所示:description

<html>
<head>
<title>test page</title>
<meta name="keywords" content="A,B,C">
<meta name="description" content="the description a page">
....

我昨天google了,但不知道,请给我一些建议。

4

1 回答 1

0

你甚至不需要scrapy来做到这一点。您只需使用标准库类即可HTMLParser

#!/usr/bin/python3
try: 
    from html.parser import HTMLParser
except ImportError:
    import HTMLParser

class MyHTMLParse(HTMLParser):
    TAG = "meta"
    NAMES = ['keywords', 'description']
    def __init__(self):
        HTMLParser.__init__(self)
        self.contents = {}
    def handle_starttag(self, tag, attrs):
        if tag == MyHTMLParse.TAG:
            attributes = {i[0] : i[1] for i in attrs}
            if attributes.get("name", None) in MyHTMLParse.NAMES:
                self.contents[attributes["name"]] = attributes["content"]


parser = MyHTMLParse()

# Feed parser the website with parser.feed(), then access the information with
# parser.contents as a dictionary with keys "keywords" and "description"
于 2015-09-08T02:28:14.967 回答