http://blog.sysuschool.com/u/mygod/index.html
ԺС
 
ԺС
2020-10-12 15:27:00
_֮requests

 

ƪСurllib⣬ƪСrequests÷עrequestsurllib.request࣬Ҫװնpip install requestsװɡ

 

requestsģ鷢ȡӦrequests.get()requests.post()ֱӿԴurlheadersparamsdataproxiesֱָurlַͷݣɰϢUser-AgentreferercookieurlIJѯֵַͣpostύݣֵͣ

ʽ磺

       requests.xxx(url=xx, headers=xx, params=xx, data=xx, proxies = xx)

       # urlԲ

 

requests.get()requests.post() صӦ󣬳õԺͷУ

       response = requests.get(https://www.baidu.com/)

       response.text   # ӦݣΪstrͣƲĽ루벻ԣܳ룩

       response.content  # Ӧݣbytesͣ .decode()ɴutf-8gbk

       response.encoding  # 뷽ʽ

       response.status_code  # Ӧ״̬

       response.request.headers  # ͷ

       response.headers  # Ӧͷݣset-cookieȣ

       response.request.url  # url

       response.url  # ӦurlһӦʱض

       response.cookies  # cookiescookies = {i.split("=")[0]:i.split("=")[1] for i in cookies.split("; ")}תֵͣҲʹ淽

       response.cookies.get_dict()  # ֵȡcookie

 

ҪtextcontentΪӦݣtextΪstrݣǾģʹõDz²ıн룬Ϊ벻Զ룬contentҲǷӦݣûнн룬bytesݣ .decode()н룬һʹutf-8

 

ȡٶȵӦݣget󣩣ģٶȡǡؼ

import requests

params={

    'wd':'й'

}

headers = {

    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}

response = requests.get('https://www.baidu.com/s',  params=params,  headers=headers)

with open('baidu.html','w',encoding='utf-8') as f:

       # contentΪbytesͣ

    f.write(response.content.decode('utf-8'))

 

ְλݣpost

import requests

# һajaxpost

url = 'https://www.lagou.com/jobs/positionAjax.json?city=%E5%B9%BF%E5%B7%9E&needAddtionalResult=false'

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',

           'referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='}

data = {

    'first': 'true',

    'pn': 1,

    'kd': 'python'

}

response = requests.post(url, data=data, headers=headers)

print(response.text)             # response.text Ϊjson

print(response.json())    # response.json() תֵ

# վjson.cnɽjson

 

ʹô

proxies = {

       "http": "http://12.34.56.79:9527",

       "https": "https://12.34.56.79:9527",

}

requests.get("http://www.baidu.com", proxies = proxies)

 

cookies session

requests ṩһsession࣬ʵֿͻ˺ͷ˵ĻỰ

       # ʵһsessionsessiongetpost

       session = requests.session()

       # ʹsession󣬵¼վcookiesession

       response = session.get(url,headers)

       # ʹsession½ܷ֮ʵվsessionܹԶЯ¼ɹʱеcookie

 

ҳ

# coding=utf-8

import requests

session = requests.session()

post_url = http://www.renren.com/PLogin.do  #

post_data = {"email":"xxx@163.com", "password":"xxx"}

headers = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"}

# ʹsessionpostcookie

session.post(post_url,data=post_data,headers=headers)

 

# ٴʹsession½ܷ֮ʵĵַ

r = session.get("http://www.renren.com/327550029/profile",headers=headers)

# ҳ

with open("renren1.html","w",encoding="utf-8") as f:

    f.write(r.content.decode())

 

# SSL֤֤⣬httpsҪ֤֤ɹر

response = requests.get("https://www.12306.cn/mormhweb/ ", verify=False)  # verify֤

 

requestsģʹСᵽˣrequests﷨࣬ʶд˼ӣʱ䣬ƪСʹseleniumԶʵ档

mygod | Ķȫ | ظ(0) | ͨ |
ۣ
ԺС
ԺС
ʱ
ԺС
־
ԺС
ԺС
»ظ
ԺС
ҵĺ
ҵ
վϢ
ԺС
ж,ڴзŷ
Powered by Oblog.