1122|urllib

http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432688314740a0aed473a39f47b09c8c7274c9ab6aee000

https://docs.python.org/3/library/urllib.request.html?highlight=request.urlopen#urllib.request.urlopen

urllib.request的说明:
https://docs.python.org/3.5/library/urllib.request.html

urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)
Open the URL url, which can be either a string or a Request object.

data must be a bytes object specifying additional data to be sent to the server, or None if no such data is needed. data may also be an iterable object and in that case Content-Length value must be specified in the headers. Currently HTTP requests are the only ones that use data; the HTTP request will be a POST instead of a GET when the data parameter is provided.


Python 3 抓取网页资源的几种办法:

1、最简单
import urllib.request
response = urllib.request.urlopen('http://python.org/')
html = response.read()

2、使用 Request
import urllib.request

req = urllib.request.Request('http://python.org/')
response = urllib.request.urlopen(req)
the_page = response.read()

3、发送数据
复制代码
#! /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]' : 'yzhang@i9i8.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"))
复制代码
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、概述 urllib2是Python的一个针对URLs的库。他以urlopen函数的形式提供了一个非常简单的...
    MiracleJQ阅读 1,524评论 0 5
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • 判断是否旋转给大家一个提示:commitAnimations记得在动画结束之后一定要commit,因为这个错误导致...
    frola_阅读 361评论 0 0
  • 年轻人能拼命,不能吃苦。 失败那么多,该怎么面对。 无限退让的软弱,不是善良;富有的不含尊重的施舍,亦不是。 是过...
    熙风空阅读 270评论 0 1
  • 瑞士,最美不过春天。 瑞士之春,最美不过花。 而我,是一个对花有执念的人。 瑞士的花,带给人的不仅仅是美,而是一种...
    ooBlessyoo阅读 705评论 0 1