I have to write a program that scrapes the website “LivingSocial” for all the details of their deals and promo and store it in MySQLdatabase.
URL : "http://www.livingsocial.com/cities/15-san-francisco"
So far I have been able to write this code :
from lxml import html
import requests
import MySQLdb
# connect
db = MySQLdb.connect(host="localhost", user="root", passwd="",
db="scrapy")
x = db.cursor()
page = requests.get('https://www.livingsocial.com/cities/15-san-francisco')
tree = html.fromstring(page.content)
#This will create a list of buyers:
descrip = tree.xpath('//p[@class="description"]/text()')
loc = tree.xpath('//p[@class="location"]/text()')
try:
x.execute("""INSERT INTO scrapy VALUES (%s,%s)""",(descrip,loc))
db.commit()
except:
db.rollback()
db.close()
I have created a MySQL database using xampp server.
But this code doesn't seem to run as expected. Please help!