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("成功!")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM