Created
August 12, 2014 05:23
Adding rows to my news table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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