Skip to content

Instantly share code, notes, and snippets.

@Aaronmyster
Created August 12, 2014 05:23
Adding rows to my news table
def addNewsArticle(companyModel,newsSourceModel,url):
tree = html.fromstring(requests.get(url).text)
# Need the article title
title = tree.xpath('//h1/text()')[0]
# Need the text of the article
textElements = tree.xpath("//*[@id='articleText']//*/text()")
text = ""
for t in textElements:
text += t
# Need the date field
dateText = tree.xpath("//div[@id='articleInfo']//span[@class='timestamp']/text()")[0]
date = datetime.datetime.strptime(dateText[:-4], "%a %b %d, %Y %I:%M%p").date()
News.create(
company=companyModel,
newsSource=newsSourceModel,
title=title,
text=text,
url=url,
date = date
)
print "Added: "+title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment