采集后的数据都带有'<>'html标签:
<img src="http://i4.hdfimg.com/www/images/giftrans/3d/da/7b/18414.gif" border="0"/><span class='WmoJPQM2AzpQMA'>科研<span class='WmoJPQM2AzhQMQ'>最早和<span class='WmoJPQM2AzxQNw'>一项<span class='WmoJPQM2AzdQOA'>教学为一体的现代化<span class='WmoJPQM2AzhQOA'>综合<span class='WmoJPQM2AzhQMQ'>师从性省级医院
在这里只要将所有带<>去除即可:
dr = re.compile(r'<[^>]+>',re.S)
dd = dr.sub('',Html)
完整的python脚本:
第一个函数:将一个字段中的刮号去除
第二个函数:将html中的所有标签去除
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import MySQLdb
import sys
import re
conn = MySQLdb.connect(host='127.0.0.1',user='user',passwd='123456',db='hospital',charset='utf8')
cur = conn.cursor()
def update_level():
cur.execute("SELECT id,level FROM hospital where level like '(%)'")
for row in cur.fetchall():
sid=row[0]
ii=re.sub('\(|\)','',row[1])
sql = "update hospital set level=%s where id=%s"
print sid,ii
param = [ii,sid]
cur.execute(sql,param)
def update_detail():
dr = re.compile(r'<[^>]+>',re.S)
cur.execute("SELECT id,details FROM hospital")
for row in cur.fetchall():
did = row[0]
detail=row[1]
dd = dr.sub('',detail)
sql="update hospital set details=%s where id=%s"
param = (dd,did)
cur.execute(sql,param)
print "Finished",did
def main():
#update_level()
update_detail()
if __name__ == '__main__':
main()