Loading

python爬取网页数据并存储到mysql数据库

#python 3.5
from urllib.request import urlopen
from urllib.request import urlretrieve
from bs4 import BeautifulSoup
import pymysql

conn = pymysql.connect(host='127.0.0.1',user='root',passwd='root',db='pmdb',charset='utf8')
cur = conn.cursor()
url = "http://www.mm131.com/xinggan/" html = urlopen(url) soup = BeautifulSoup(html,"lxml") urllist = soup.find(class_='list-left public-box') for i in urllist: jlist = soup.find("dd").find("a")["href"] jliststr='\''+jlist+'\'' # print(jlist) cur.execute("insert into href(href) VALUES ("+jliststr+")") conn.commit() cur.close() conn.close() print("成功!")
posted @ 2018-01-19 13:39  江雪独钓翁  阅读(6029)  评论(0编辑  收藏  举报