Extract Meta Keywords From Webpage?
Asked Answered
B

3

8

I need to extract the meta keywords from a web page using Python. I was thinking that this could be done using urllib or urllib2, but I'm not sure. Anyone have any ideas?

I am using Python 2.6 on Windows XP

Baseburner answered 9/7, 2010 at 19:15 Comment(1)
Make sure to use caching of the contents whenever possible developer.yahoo.com/python/python-caching.htmlCentering
S
11

lxml is faster than BeautifulSoup (I think) and has much better functionality, while remaining relatively easy to use. Example:

52> from urllib import urlopen
53> from lxml import etree

54> f = urlopen( "http://www.google.com" ).read()
55> tree = etree.HTML( f )
61> m = tree.xpath( "//meta" )

62> for i in m:
..>     print etree.tostring( i )
..>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-2"/>  

Edit: another example.

75> f = urlopen( "http://www.w3schools.com/XPath/xpath_syntax.asp" ).read()
76> tree = etree.HTML( f )
85> tree.xpath( "//meta[@name='Keywords']" )[0].get("content")
85> "xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,b
eginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading
 style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide"

BTW: XPath is worth knowing.

Another edit:

Alternatively, you can just use regexp:

87> f = urlopen( "http://www.w3schools.com/XPath/xpath_syntax.asp" ).read()
88> import re
101> re.search( "<meta name=\"Keywords\".*?content=\"([^\"]*)\"", f ).group( 1 )
101>"xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql, ...etc...

...but I find it less readable and more error prone (but involves only standard module and still fits on one line).

Sumbawa answered 9/7, 2010 at 19:34 Comment(3)
Ok, but where are the keywords of the document. I need to check the keywords in the meta data against a list I have.Baseburner
As you can see they are in 'content' attribute of <meta> tag which 'name' attribute is 'Keywords' :)Sumbawa
Also make sure to use caching of the contents whenever possible developer.yahoo.com/python/python-caching.htmlCentering
B
7

BeautifulSoup is a great way to parse HTML with Python.

Particularly, check out the findAll method: http://www.crummy.com/software/BeautifulSoup/documentation.html

Butyraceous answered 9/7, 2010 at 19:17 Comment(0)
W
0

Why not use a regular expression

keywordregex = re.compile('<meta\sname=
["\']keywords["\']\scontent=["\'](.*?)["\']\s/>')

keywordlist = keywordregex.findall(html)
if len(keywordlist) > 0:
    keywordlist = keywordlist[0]
    keywordlist = keywordlist.split(", ")
Wrier answered 23/10, 2013 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.