这是一段抓取天气预报的脚本,参考sanniandegudan的,我主要是用来练习Python下怎么对Mysql语句进行操作。
import urllib
import re
import MySQLdb
def get_weather(city = '上海'):
# Request For The Web Page
params = urllib.urlencode({'city':city})
webpage = urllib.urlopen('http://weather.news.sohu.com/city_inc.php?%s' % params)
webdata = webpage.read().replace('\n', '')
# Match The Useful Information
pattern = re.compile('
([^([^([^([^db=MySQLdb.connect(host='localhost', #连接数据库
user='test',
passwd='test',
db='mytable',
charset='gb2312') #设置字符编码
cursor=db.cursor()
cursor.execute ("DROP TABLE IF EXISTS message")
cursor.execute ("""
CREATE TABLE message