对书名的抓取
import urllib as request
import sys
sys.path.append('E:/software/python/beautifulsoup4-4.5.3') //---------这里要引在本地安装的的beautifulsoup4,其作用看本python类博客
sys.path.append('E:/software/python/Lib/site-packages') //---------这里要引在本地安装的的python的lib库
from bs4 import BeautifulSoup
import MySQLdb
import re
res = request.urlopen("http://www.douban.com/tag/%E5%B0%8F%E8%AF%B4/?focus=book")
soup = BeautifulSoup(''.join(res.read()),'html.parser')
#print soup
book_div = soup.find(attrs={"id":"book"})
book_a = book_div.findAll(attrs={"class":"title"})
connection = MySQLdb.connect(host="localhost",user="root",passwd="123456",db="kunlun",port=3306,charset="utf8")
cursor = connection.cursor()
for book in book_a:
print book.string
sql ="INSERT INTO books(bookname) VALUES ('%s') " %(book.string)
#sql ="INSERT INTO books(bookname)VALUES (%s)" %("'"+book.string+"'")
sql_res = cursor.execute(sql)
connection.commit()
cursor.close()
connection.close()