1. 程式人生 > >學習筆記 requests + BeautifulSoup

學習筆記 requests + BeautifulSoup

hub CA ade row 4.2 enc head keep clas

第一步:requests

get請求

# -*- coding:utf-8  -*-
# 日期:2018/5/15 17:46
# Author:小鼠標
import requests
url = "http://www.baidu.com"
#res = requests.get(url)  #方法1

res = requests.request(‘get‘,url) #方法2
print(響應狀態碼:,res.status_code) print(響應內容:,res.text)

post請求

# -*- coding:utf-8  -*-
# 日期:2018/5/15 17:46
# Author:小鼠標
import requests url = "http://www.baidu.com" data = { username: xiaoshubiao, pwd: xiaoshubiao } res = requests.post(url,data) print(響應狀態碼:,res.status_code) print(響應內容:,res.text)

第二步:偽裝瀏覽器和偽造cookie

# -*- coding:utf-8  -*-
# 日期:2018/5/15 17:46
# Author:小鼠標
import requests
url = "http://www.baidu.com
" headers = {User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.39 64.2 Safari/537.36, Accept: text/html,application/xhtml+xml,application/xml;q=0
.9,image/webp,*/*;q=0.8, Accept-Encoding: gzip, deflate, sdch, Accept-Language: zh-CN,zh;q=0.8,en;q=0.6, Cache-Control: max-age=0, Connection: keep-alive } cookies = dict(name=xiaoshubiao) res = requests.get(url,headers = headers,cookies = cookies) print(響應狀態碼:,res.status_code) print(響應內容:,res.text)

第三步:使用代理ip

# -*- coding:utf-8  -*-
# 日期:2018/5/15 17:46
# Author:小鼠標
import requests
url = "http://www.baidu.com"
headers = {User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36
                          (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.39
                         64.2 Safari/537.36,
            Accept: text/html,application/xhtml+xml,application/xml;q=0
                      .9,image/webp,*/*;q=0.8,
            Accept-Encoding: gzip, deflate, sdch,
            Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,
            Cache-Control: max-age=0,
            Connection: keep-alive
            }
cookies = dict(name=xiaoshubiao)
proxies = {http:218.73.134.234:36602}
res = requests.get(url,headers = headers,cookies = cookies,proxies = proxies)
print(響應狀態碼:,res.status_code)
print(響應內容:,res.text)

學習筆記 requests + BeautifulSoup