Hi. I am writing some quick-n-dirty code to scrape together a RSS feed
from a section of the ITV website, and am stuck with this persistent
error. If I try to open the resultant xml file in Firefox, it errors
out and is unable to recognize the text pulled from the <h1> tags of
http://www.itv-f1.com/Feature.aspx?Type=Gravel_Trap&id=41916 --
'It’s a hard lifeâ€'. I am not sure what encoding
this is, the webpage doesnt mention it, and a query of
handle.headers.getencoding() gives '7bit'.
Here is the code:
import urllib2
from BeautifulSoup import BeautifulSoup
def getcontents(article_link):
""" Given the URL of a gravel trap article, this will return the
title of that article. """
handle = urllib2.urlopen(article_link)
soup = BeautifulSoup(handle.read())
article_heading = str(soup('h1')) # get the only <H1> tag in the
page
return article_heading
if __name__ == '__main__':
rssfile = open('C:/Users/xyz/Desktop/graveltraprss.xml','wb')
article_url = '
http://www.itv-f1.com/Feature.aspx?
Type=Gravel_Trap&id=41916'
contents = getcontents(article_url)
rssfile.write(
"""<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Gravel Trap - ITV.com/F1</title>
<link>
http://www.itv-f1.com/Feature.aspx?
Type=Gravel_Trap</link>
<description>The light hearted side of F1.</description>
<language>en-us</language>
<pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
<lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</
lastBuildDate>
<docs>
http://blogs.law.harvard.edu/tech/rss</docs>
<generator>Web scrapped by Python & BeautifulSoup</
generator>
<managingEditor>
x...@yahoo.com</managingEditor>
<webMaster>
x...@yahoo.com</webMaster>""")
rssfile.write(
""" <item>
<title>""" + contents + """</title>
<link>%s</link>
<description>blank</description>
<pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
<guid>
http://liftoff.msfc.nasa.gov/
2003/05/20.html#item570</guid>
</item>""" % article_url)
rssfile.write('</channel></rss>')
rssfile.close()
I have tried a million things: tried appending a 'u' before the
strings, opening the file using codecs.open(), pickle, binary & the
default file opening modes ... nothing seems to work.
Please help.
Thanks
- LJ