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

时间:2022-09-18 17:54:39
#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("成功!")