【python学习.油价和美元汇率查询】
16lz
2021-01-22
这周的编程任务不难,找到的都是静态网页,用正则表达式查询下就可以了,就是转换码的时候费了我好大劲。
原网页的代码如下:
__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])
更多相关文章
- 谈网页游戏外挂之用python模拟游戏(热血三国2)登陆
- 简单的python爬取网页字符串内容并保存
- python python 入门学习之网页数据爬虫cnbeta文章保存
- 用 Python requests库 爬取网页数据
- python爬网页,做k线图
- 【python 编程】网页中文过滤分词及词频统计
- Python MOOC简单获取网页内容并以html格式保存在本地
- 【Python】python3实现网页爬虫下载图片
- python进阶——利用网页爬虫写天气预报采集器