【python学习.油价和美元汇率查询】

这周的编程任务不难,找到的都是静态网页,用正则表达式查询下就可以了,就是转换码的时候费了我好大劲。

原网页的代码如下:




__author__ = 'Administrator'
# -*- coding: UTF-8 -*-
import urllib2
import re
import time
import MySQLdb




def oil():
    conn=MySQLdb.connect(host='localhost',user='cjw',passwd='123456',port=3306)
    cur=conn.cursor()
    conn.select_db('python1')




    url="http://www.bitauto.com/youjia/suzhou/?WT.srch=1"
    req=urllib2.Request(url)
    response=urllib2.urlopen(req)
    page=response.read()


    req1=re.compile('class="todayPrice"><strong>(.*?)</strong>(.*?)</span>',re.X)
    req2=re.compile('class="oilNum">(.*?)</span>',re.X)
    match=req1.findall(page)
    match2=req2.findall(page)
    cur.execute('insert into week6oil values(%s)',match[0][0])
    conn.commit()
    cur.close()
    conn.close()
    a=0
    for j in match2:
        j=j.decode("utf-8")
        print j
        c= match[a][1].decode('utf-8')
        print match[a][0]+c
        a=a+1






def money():
    conn=MySQLdb.connect(host='localhost',user='cjw',passwd='123456',port=3306)
    cur=conn.cursor()
    conn.select_db('python1')
    cur.execute('create table week6money4(huilv varchar(20)) ')
    url='http://huilv.911cha.com/USDCNY.html'
    req=urllib2.Request(url)
    response=urllib2.urlopen(req)
    page=response.read()
    req1=re.compile('<p>(.*?)</p>')
    req1=re.compile('<p>美元对人民币汇率:(.*?)</p>')
    match=req1.findall(page)
    match2=req1.findall(page)


    cur.execute('insert into week6money4 values(%s)',match2)
    conn.commit()
    cur.close()
    conn.close()
    for i in match:
        i=i.decode("utf-8")
        print i




try:
 while(True):
     oil()
     money()


     time.sleep(3600)
except MySQLdb.Error,e:
     print "Mysql Error %d: %s" % (e.args[0], e.args[1])
然后这是结果



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值