I am new to crawler and I am using Python 3.X. Currently I am practicing to crawl google news for fresh start but I have encounter some problem with my code(the code runs but did not return anything). I want the code to crawl google news for query and return results with url, title and briefing appear in results.
Many thanks for your time. my code is below:
import sys
import urllib
import requests
from bs4 import BeautifulSoup
import time
s = "Stack Overflow"
url = "http://www.google.com.sg/search?q="+s+"&tbm=nws&tbs=qdr:y"
#htmlpage = urllib2.urlopen(url).read()
time.sleep(randint(0, 2))
htmlpage = requests.get(url)
soup = BeautifulSoup(htmlpage.text,'lxml')
#print (len(soup.findAll("table", {"class": "result"})))
for result_table in soup.findAll("table", {"class": "result"}):
a_click = result_table.find("a")
print ("-----Title----\n" + a_click.renderContents())#Title
print ("----URL----\n" + str(a_click.get("href")))#URL
print ("----Brief----\n" + result_table.find("div", {"class": "c-abstract"}).renderContents())#Brief
print ("Done")