美文网首页
Python3的urllib防止403 forbidden re

Python3的urllib防止403 forbidden re

作者: 这个年纪的情愫丶 | 来源:发表于2017-09-22 10:57 被阅读370次
    How to import urllib.request and urllib.parse:
    import urllib.request as urlRequest
    import urllib.parse as urlParse
    
    1.How to make a GET request:
    url = "http://www.example.net"
    # open the url
    x = urlRequest.urlopen(url)
    # get the source code
    sourceCode = x.read()
    
    2.How to make a POST request:
    url = "https://www.example.com"
    values = {"key": "python"}
    # encode values for the values
    values = urlParse.urlencode(values)
    # encode the values in UTF-8 format
    values = values.encode("UTF-8")
    # create the url
    targetUrl = urlRequest.Request(url, values)
    # open the url
    x  = urlRequest.urlopen(targetUrl)
    # get the source code
    sourceCode = x.read()
    
    3.How to make a POST request (403 forbidden responses):
    url = "https://www.example.com"
    values = {"q": "python urllib"}
    # pretend to be a chrome 47 browser on a windows 10 machine
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"}
    # encode values for the url
    values = urlParse.urlencode(values)
    # encode the values in UTF-8 format
    values = values.encode("UTF-8")
    # create the url
    targetUrl = urlRequest.Request(url = url, data = values, headers = headers)
    # open the url
    x  = urlRequest.urlopen(targetUrl)
    # get the source code
    sourceCode = x.read()
    
    4.How to make a GET request (403 forbidden responses):
    url = "https://www.example.com"
    # pretend to be a chrome 47 browser on a windows 10 machine
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"}
    req = urlRequest.Request(url, headers = headers)
    # open the url
    x = urlRequest.urlopen(req)
    # get the source code
    sourceCode = x.read()
    

    相关文章

      网友评论

          本文标题:Python3的urllib防止403 forbidden re

          本文链接:https://www.haomeiwen.com/subject/opnxextx.html