码迷,mamicode.com
首页 > 其他好文 > 详细

对书名的抓取

时间:2017-11-04 13:17:47      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:mysq   int   cal   insert   package   values   抓取   .exe   ldb   

import urllib
import sys
sys.path.append(‘D:/python/beautifulsoup‘)
sys.path.append(‘C:/Python27/Lib/site-packages‘)
from bs4 import BeautifulSoup
import MySQLdb
import re
res = urllib.urlopen("http://www.douban.com/tag/%E5%B0%8F%E8%AF%B4/?focus=book")

soup = BeautifulSoup(‘‘.join(res.read()))
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="test_a",port=3306,charset="utf8")
cursor = connection.cursor()
for book in book_a:
sql = "INSERT INTO books(bookname) \
VALUES (‘%s‘)" % \
(book.string)
sql_res = cursor.execute(sql)
connection.commit()
cursor.close()
connection.close()

对书名的抓取

标签:mysq   int   cal   insert   package   values   抓取   .exe   ldb   

原文地址:http://www.cnblogs.com/kongxc/p/7782832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!