python3 抓取网页资源的 N 种方法
import urllib.request
response = urllib.request.urlopen('http://python.org/')
html = response.read()
import urllib.request req = urllib.request.Request('http://python.org/')
response = urllib.request.urlopen(req)
the_page = response.read()
#! /usr/bin/env python3 import urllib.parse
import urllib.request url = 'http://localhost/login.php'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {
'act' : 'login',
'login[email]' : '12345@qq.com',
'login[password]' : '123456'
} data = urllib.parse.urlencode(values)
req = urllib.request.Request(url, data)
req.add_header('Referer', 'http://www.python.org/')
response = urllib.request.urlopen(req)
the_page = response.read() print(the_page.decode("utf8"))
#! /usr/bin/env python3 import urllib.parse
import urllib.request url = 'http://localhost/login.php'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {
'act' : 'login',
'login[email]' : '12334@qq.com',
'login[password]' : '123456'
}
headers = { 'User-Agent' : user_agent } data = urllib.parse.urlencode(values)
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)
the_page = response.read() print(the_page.decode("utf8"))
#! /usr/bin/env python3 import urllib.request req = urllib.request.Request('http://www.python.org/fish.html')
try:
urllib.request.urlopen(req)
except urllib.error.HTTPError as e:
print(e.code)
print(e.read().decode("utf8"))
#! /usr/bin/env python3 from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
req = Request("http://twitter.com/")
try:
response = urlopen(req)
except HTTPError as e:
print('The server couldn\'t fulfill the request.')
print('Error code: ', e.code)
except URLError as e:
print('We failed to reach a server.')
print('Reason: ', e.reason)
else:
print("good!")
print(response.read().decode("utf8"))
#! /usr/bin/env python3 from urllib.request import Request, urlopen
from urllib.error import URLError
req = Request("http://twitter.com/")
try:
response = urlopen(req)
except URLError as e:
if hasattr(e, 'reason'):
print('We failed to reach a server.')
print('Reason: ', e.reason)
elif hasattr(e, 'code'):
print('The server couldn\'t fulfill the request.')
print('Error code: ', e.code)
else:
print("good!")
print(response.read().decode("utf8"))
#! /usr/bin/env python3 import urllib.request # create a password manager
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() # Add the username and password.
# If we knew the realm, we could use it instead of None.
top_level_url = "https://cms.tetx.com/"
password_mgr.add_password(None, top_level_url, 'yzhang', 'cccddd') handler = urllib.request.HTTPBasicAuthHandler(password_mgr) # create "opener" (OpenerDirector instance)
opener = urllib.request.build_opener(handler) # use the opener to fetch a URL
a_url = "https://cms.tetx.com/"
x = opener.open(a_url)
print(x.read()) # Install the opener.
# Now all calls to urllib.request.urlopen use our opener.
urllib.request.install_opener(opener) a = urllib.request.urlopen(a_url).read().decode('utf8')
print(a)
#! /usr/bin/env python3 import urllib.request proxy_support = urllib.request.ProxyHandler({'sock5': 'localhost:1080'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener) a = urllib.request.urlopen("http://g.cn").read().decode("utf8")
print(a)
#! /usr/bin/env python3 import socket
import urllib.request # timeout in seconds
timeout = 2
socket.setdefaulttimeout(timeout) # this call to urllib.request.urlopen now uses the default timeout
# we have set in the socket module
req = urllib.request.Request('http://twitter.com/')
a = urllib.request.urlopen(req).read()
print(a)
python3 抓取网页资源的 N 种方法的更多相关文章
- python3爬取网页
爬虫 python3爬取网页资源方式(1.最简单: import'http://www.baidu.com/'print2.通过request import'http://www.baidu.com' ...
- php抓取网页中的内容
以下就是几种常用的用php抓取网页中的内容的方法.1.file_get_contentsPHP代码代码如下:>>>>>>>>>>>&g ...
- php抓取网页
用php抓取页面的内容在实际的开发其中是很实用的,如作一个简单的内容採集器,提取网页中的部分内容等等.抓取到的内容在通过正則表達式做一下过滤就得到了你想要的内容.下面就是几种经常使用的用php抓取网页 ...
- Python3简单爬虫抓取网页图片
现在网上有很多python2写的爬虫抓取网页图片的实例,但不适用新手(新手都使用python3环境,不兼容python2), 所以我用Python3的语法写了一个简单抓取网页图片的实例,希望能够帮助到 ...
- Python3抓取javascript生成的html网页
用urllib等抓取网页,只能读取网页的静态源文件,而抓不到由javascript生成的内容. 究其原因,是因为urllib是瞬时抓取,它不会等javascript的加载延迟,所以页面中由javasc ...
- python3下scrapy爬虫(第四卷:初步抓取网页内容之抓取网页里的指定数据延展方法)
上卷中我运用创建HtmlXPathSelector 对象进行抓取数据: 现在咱们再试一下其他的方法,先试一下我得最爱XPATH 看下结果: 直接打印出结果了 我现在就正常拼下路径 只求打印结果: 现在 ...
- [Python]网络爬虫(一):抓取网页的含义和URL基本构成
一.网络爬虫的定义 网络爬虫,即Web Spider,是一个很形象的名字. 把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛.网络蜘蛛是通过网页的链接地址来寻找网页的. 从网站某一个 ...
- python抓取网页引用的模块和类
在Python3.x中,我们可以使用urlib这个组件抓取网页,urllib是一个URL处理包,这个包中集合了一些处理URL的模块,如下:1.urllib.request模块用来打开和读取URLs:2 ...
- PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)
通过curl_setopt()函数可以方便快捷的抓取网页(采集很方便),curl_setopt 是php的一个扩展库 使用条件:需要在php.ini 中配置开启.(PHP 4 >= 4.0.2) ...
随机推荐
- struts2 用if标签判断字符串包含
String testStr = "用来判断是否包含的字符串"; <s:property value="testStr"/> <s:if te ...
- ASP.NET Core--授权过滤器
翻译如下: 目前,我们正在从事这方面工作. 我们欢迎您的加入,以帮助塑造范围和方法.您可以跟踪状态并在此提供的输入问题在GitHub上. 如果你想查看初稿并在此主题的大纲,请留下注意到在您的联系信息的 ...
- SVN版本控制与分支设置
使用SVN+Eclipse做软件版本控制. (2009年5月) 1,SVN目录结构 Trunk : 主干目录,此目录下的文件为基准文件 Branches : 用于开发的分支目录 Tags : 用于发布 ...
- git 简单使用
创建新分支:git branch branchName 切换到新分支:git checkout branchName 然后 ,上面两个命令也可以合成为一个命令: git checkout -b bra ...
- StartUML2.8破解
StarUML官方下载地址:http://staruml.io/download StarUML是一个非常好用的画UML图的工具,但是它是收费软件,以下是破解方法: 1.使用Editplus或者N ...
- CentOS防火墙iptables的配置方法详解
CentOS系统也是基于linux中的它的防火墙其实就是iptables了,下面我来介绍在CentOS防火墙iptables的配置教程,希望此教程对各位朋友会有所帮助. iptables是与Linux ...
- python3 黑板客爬虫闯关游戏(一)
这是学习python爬虫练习很好的网站,强烈推荐! 地址http://www.heibanke.com/lesson/crawler_ex00/ 第一关猜数字 很简单,直接给出代码 import ur ...
- SVN 删除误上传到服务器的文件
使用Axure软件的时候,不小心把一些无用的文档也提交到了SVN上了. 当更新服务器上的文件到本地,然后删除误提交的文件时,出现了一个错误,见下图: 错误:cannot verify lock o ...
- cf596d
题意:有一排等高的树木,高度都为h.给出每棵树在数轴上的坐标,每次有可能是最左边或者最右边的立着的树倒下,概率都是0.5.最终所有树都倒下.每棵树在倒下的时候有p的概率向左倒,1-p的概率向右倒.如果 ...
- 无法启动"D\projects\hello\Debug\hello.exe" 系统找不到指定的文件。[LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏]
这两天安装Visual Studio遇到这样的一个问题,用自己的电脑和公司的电脑都出现同样的问题.两台电脑都是新系统,按理来说是没有问题的.但是一出现问题,对于我这个小白来说,还是耗费了挺多精力都无果 ...