python:爬蟲之Post請求以及動態Ajax資料的爬取(3)
阿新 • • 發佈:2018-11-17
#爬蟲的post方式
作用:對引數進行打包反饋給伺服器
import urllib.request import urllib.parse #對引數打包 url = "http://www.sunck.wang:8085/form" data = { "username":"sunck", "passwd":"123" } postdata = urllib.parse.urlencode(data).encode("utf-8") #對data打包合成一個字典 #建立請求體 req = urllib.request.Request(url,data=postdata) #發起請求,對req新增一個請求頭 req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0") response = urllib.request.urlopen(req) print(response.read().decode("utf-8"))
#抓取動態頁面
def AjaxDemo(url) : headers ={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" } req = urllib.request.Request(url,headers=headers) context =ssl._create_unverified_context() #使用ssl建立未驗證的上下資料 response = urllib.request.urlopen(req,context) JsonStr = response.read().decode("utf-8") data = json.loads(JsonStr) return data url = "movie.douban.com/動態/" #模擬動態url print(AjaxDemo(url))