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

原网页的代码如下:




__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. 谈网页游戏外挂之用python模拟游戏(热血三国2)登陆
  2. 简单的python爬取网页字符串内容并保存
  3. python python 入门学习之网页数据爬虫cnbeta文章保存
  4. 用 Python requests库 爬取网页数据
  5. python爬网页,做k线图
  6. 【python 编程】网页中文过滤分词及词频统计
  7. Python MOOC简单获取网页内容并以html格式保存在本地
  8. 【Python】python3实现网页爬虫下载图片
  9. python进阶——利用网页爬虫写天气预报采集器

随机推荐

  1. python题目——认识*与**,判断函数输出
  2. Python 基础第五天
  3. 支持c和python之间的跨语言(c)标记的代码
  4. 尝试使用python字典重新格式化JSON数据
  5. 使用pip安装tensorflow 0.80,python 使用t
  6. 显示一个矩阵,将一个共同因素放在同情中
  7. python通过get方式,post方式发送http请求
  8. 不同程序语言之间的互动,IronPython 与C#
  9. Python语言特性之1:函数参数传递
  10. [置顶] Python + C/C++ 嵌入式编