Fork me on GitHub

urllib基础

python爬虫,突然想学一学,导入urllib后开始愉快地玩耍。 https://httpbin.org 是开发者测试网站,调试向服务器请求的代码。

 

import urllib.request
import urllib.parse
import ssl

ssl._create_default_https_context = ssl.SSLContext


def main():
    req = urllib.request.Request('https://httpbin.org/post')    # 请求url
    req.add_header('User-Agent',    # 更改请求方从python到browser
                   'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26'
                   ' (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
    data = bytes(urllib.parse.urlencode({"hello": "world"}), encoding="utf-8")  # post提交的信息
    response = urllib.request.urlopen(req, data=data)   # 打开url并提交data
    print('Status:', response.status, response.reason)  # 返回状态码
    print(response.read().decode("utf-8"))  # 返回响应体
    for k, v in response.getheaders():  # 返回响应头
        print('%s: %s' % (k, v))


if __name__ == '__main__':
    main()

 

posted @ 2021-04-11 23:50  Faded828x  阅读(55)  评论(0编辑  收藏  举报