# 解析html 插入数据到mysql ## 包 ```bash $ pip3 install beautifulsoup4 $ pip3 install lxml $ pip3 install html5lib $ pip3 install pymysql ``` # 代码 ```python3 #!/usr/bin/env python3 import pymysql from bs4 import BeautifulSoup import time # 打开HTML文件 (或用requests) f = open('index.html', 'r'); fcontent = f.read() f.close() # 解析 dom = BeautifulSoup(fcontent, 'lxml') cases = dom.select("div.section02 > div > div.index-case > ul > li") # 对返回的列表进行遍历 insertData = []; for item in cases: tmp = { 'first_img': '/static/mdx/'+item.select('div.pic > img')[0].get('src'), 'name': item.select('a > h2 > span')[0].get_text(), 'content': item.select('a > h2 > span')[0].get_text(), 'url': '#', 'create_time': str(int(time.time())), 'update_time':str(int(time.time())), } insertData.append(tuple(tmp.values())) # 保存数据库 db = pymysql.connect("localhost","web_edk24_com","6666","web_edk24_com" ) cursor = db.cursor() sql = "INSERT INTO cases (first_img, name, content, url, create_time, update_time) VALUES(%s, %s, %s, %s, %s, %s)" cursor.executemany(sql, tuple(insertData)) db.commit() db.close() ```