使用Python3 urllib.request中的Requests()和urlopen()方法获取页面源码,并用re正则进行正则匹配查找需要的数据。

#forex.py
#coding:utf-8
'''
urllib.request.urlopen() function in Python 3 is equivalent to urllib2.urlopen() in Python2
urllib.request.Request() function in Python 3 is equivalent to urllib2.Request() in Python2
'''
#python3.5
import urllib.request
#python2.7
#import urllib
#import urllib2 import re def Gethtml(url, referer):
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:53.0) Gecko/20100101 Firefox/53.0"
headers={"User-agent":user_agent,'referer':referer}
#python3.5
req=urllib.request.Request(url,headers=headers)
response=urllib.request.urlopen(req,timeout=10)
#python2.7
#req=urllib2.Request(url,headers=headers)
#response=urllib2.urlopen(req,timeout=10)
return response.read() url=referer="http://quote.forex.hexun.com/EURUSD.shtml"
html = str(Gethtml(url, referer))
reg = r'([0-1]{1}\.[0-9]{4})'
i = re.compile(reg)
r = re.findall(i, html)
print("Hexun ERUUSD:\nCur | Open | Yesterday | Low | High")
print(r)

运行:python forex.py

输出:

Hexun ERUUSD:
Cur   |     Open |  Yesterday  |  Low  |  High
['1.1278', '1.1211', '1.1211', '1.1203', '1.1285']

referer是反盗链,服务器会识别headers中的referer是不是它自己,如果不是,有的服务器不会响应,timeout=10 是超时设定。
参考:

爬虫小探-Python3 urllib.request获取页面数据的更多相关文章

  1. Python3 urllib.request库的基本使用

    Python3 urllib.request库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地. 在Python中有很多库可以用来抓取网页,我们先学习urlli ...

  2. Python3.x:定时获取页面数据存入数据库

    Python3.x:定时获取页面数据存入数据库 #间隔五分钟采集一次数据入库 import pymysql import urllib.request from bs4 import Beautifu ...

  3. 【转】python3 urllib.request 网络请求操作

    python3 urllib.request 网络请求操作 基本的网络请求示例 ''' Created on 2014年4月22日 @author: dev.keke@gmail.com ''' im ...

  4. python3 urllib.request 网络请求操作

    python3 urllib.request 网络请求操作 基本的网络请求示例 ''' Created on 2014年4月22日 @author: dev.keke@gmail.com ''' im ...

  5. 在Servlet端获取html页面选中的checkbox值,request获取页面checkbox(复选框)值

    html端代码: 选项框: <input type="checkbox" name="crowd" value="选项一">选项 ...

  6. 获取WebBrowser全cookie 和 httpWebRequest 异步获取页面数据

    获取WebBrowser全cookie [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true) ...

  7. 爬虫初探(1)之urllib.request

    -----------我是小白------------ urllib.request是python3自带的库(python3.x版本特有),我们用它来请求网页,并获取网页源码. # 导入使用库 imp ...

  8. (转)python3 urllib.request.urlopen() 错误UnicodeEncodeError: 'ascii' codec can't encode characters

    代码内容: url = 'https://movie.douban.com/j/search_subjects?type=movie'+ str(tag) + '&sort=recommend ...

  9. 爬虫第一篇:爬虫详解之urllib.request模块

    我将urllib.request 的GET请求和POST请求两种方法做了总结 GET请求 GET请求爬取: import urllib.request import urllib.parse head ...

随机推荐

  1. Java注解学习

    一.注解定义 JVM5.0定义了4个标准的元注解: @Target, @Retention, @Documented @Inherited 1. @Target 作用:用于描述注解的使用范围 取值El ...

  2. 【Webpack的使用指南 02】Webpack的常用解决方案

    说是解决方案实际上更像是webpack的插件索引. 写这一篇的目的是为了形成一个所以,将来要用时直接来查找即可. 索引目录 1.自动构建HTML,可压缩空格,可给引用的js加版本号或随机数:html- ...

  3. centos6.5下 hdp-2.4.2安装

    (1)准备工作 /usr/sbin/sestatus -v getenforce1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态SE ...

  4. 探究c# lock

    今天早上阅读前辈的代码,看到了这么一段代码,如下所示: lock("Execute") { string sqlStr = sbSQLScript.ToString(); } 看到 ...

  5. Health Check - 每天5分钟玩转 Docker 容器技术(142)

    强大的自愈能力是 Kubernetes 这类容器编排引擎的一个重要特性.自愈的默认实现方式是自动重启发生故障的容器.除此之外,用户还可以利用 Liveness 和 Readiness 探测机制设置更精 ...

  6. 22.jQuery(实例)

    1.开关灯效果 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  7. 小米google play service停止工作解决办法,不root,不刷第三方recovery(也适用于其他的手机)

    问题: 原因是手机安卓系统是6.0.系统应用里面没有包含谷歌框架等一系列谷歌的小东西. 参考: http://www.miui.com/thread-3548436-1-1.html http://w ...

  8. python 对模块的应用你还得练点这些

    1.有如下字符串:n = "路飞学城"(编程题) - 将字符串转换成utf-8的字符编码的字节,再将转换的字节重新转换为utf-8的字符编码的字符串 - 将字符串转换成gbk的字符 ...

  9. 用golang 实现一个代理池

    背景 写爬虫的时候总会遇到爬取速度过快而被封IP的情况,这个时候就需要使用代理了.在https://github.com/henson/ProxyPool 的启发下,决定自己实现一个代理池.项目已经开 ...

  10. poj1321 棋盘(dfs)

    #include<iostream> #include<cstring> using namespace std; ]={},sum=; ][]; void dfs(int a ...