python学习3

方法1 url直接写参数

import requests

url = ‘https://www.baidu.com/s?wd=python’

header = {‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36’}

res = requests.get(url, headers=header)

with open(‘baidu.html’, ‘wb’) as f:

f.write(res.content)

#方法2 构建参数字典

import requests

url = ‘https://www.baidu.com/s?’

header = {‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36’}

tmp = ‘zhishiTopicRequestTime=1705995643553; BIDUPSID=5541BFB2E2961AA6674E88C2F8581F95; PSTM=1698724511; ZFY=zHQgCiiOBWbajO1WHUhzxtWxMTidg7HD5Rlf5V5t4LM:C; H_PS_PSSID=40018; BDUSS_BFESS=Q4TFlLd003N3dNb2p0bTVMbnlzazZxQ0g4Y2NnUlp2Zkt2SmhuMkVFQ1NtOVZsRUFBQUFBJCQAAAAAAAAAAAEAAAD~htArYmFieb2qu7POsAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJIOrmWSDq5lSj; BA_HECTOR=8g208g848l01212025ak8kalnmpp601iqs7do1s; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=1; delPer=0; BAIDUID=5541BFB2E2961AA61FB172FF203135CF:SL=0:NR=10:FG=1; BAIDUID_BFESS=5541BFB2E2961AA61FB172FF203135CF:SL=0:NR=10:FG=1; channel=baidusearch; H_WISE_SIDS=40018; ab_sr=1.0.1_MDdmZjUyMjdlMjEyNmMyOTQwYjZjYTFlZjYwOTJiNGI4OGU1ZmJiMDQzZmYxMGM1NzlkMDZjN2Y3ZTJmYjExMzdlZTI4M2M5NzEyZTMxMmQzMzMwNDMxMTc5MzRlMDQxMzJlMTEzNmVmZjZmMzAyYTc3YjIwMjIwMjZjZDcyNGFhNGI0OWQzNjliY2UwZTg3ZWZlZDQyMTQ0MjYyZWIzMjA4MTI2MzM1MmFiNGY1ODRmZTc5Mjc1MzljYmFiMGU4’

cookieList = tmp.split('; ')

cookies = {}

for cookie in cookieList:

cookies[cookie.split(‘=’)[0]] = cookie.split(‘=’)[-1]

print(cookie)

print(cookies)

data = {‘wd’: ‘python’}

res = requests.get(url, headers=header,params=data)

print(res.url)

with open(‘baidu1.html’, ‘wb’) as f:

f.write(res.content)

cookiesJar的学习

import requests

import utils

url = ‘https://www.baidu.com’

res = requests.get(url)

print(res.cookies)

dictCookies = requests.utils.dict_from_cookiejar(res.cookies)

print(dictCookies)

# timeout学习

import requests

# twitter一般访问不了

url = ‘https://twitter.com’

res = requests.get(url, timeout=3)

print(res)

代理的学习, 代理相当于快递员,作用是转发请求

import requests

url = “http://www.baidu.com”

proxy = {‘https’: ‘http://122.239.188.215:17612’}

res = requests.get(url, proxies=proxy)

print(res.text)

verify的学习

import requests

url = “https://sam.huat.edu.cn:8443/selfservice/”

res = requests.get(url, verify=False)

print(res.content)

https://www.iciba.com/word?w=extrapolation

在响应这个标签里面找

{“message”:[{“key”:“字典”,“paraphrase”:“dictionary;wordbook “,“value”:0,“means”:[{“part”:””,“means”:[“dictionary”,"wordbook "]}]}],“status”:1}

# post请求

import requests

import json

# https://www.iciba.com/word?w=%E6%A1%8C%E5%AD%90

class King(object):

def init(self, word):

# refer

self.url = ‘https://www.iciba.com/word?’

self.headers = {‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36’}

self.data = {‘w’: word}

def getData(self):

res = requests.get(self.url, data=self.data, headers=self.headers)

return res.content

def parseData(self, data):

dicData = json.load(data)

print(dicData[‘message’][‘means’])

def run(self):

res = self.getData()

print(res)

if name == ‘main’:

king = King(‘字典’)

king.run()

requests.session

import requests
session = requests.session()

你可能感兴趣的:(python,学习,开发语言)