首页 > 编程语言 >Python爬虫

Python爬虫

时间:2023-01-09 09:22:21浏览次数:44  
标签:headers Python resp get 爬虫 url requests self

参考网址

https://blog.csdn.net/qq_35709559/article/details/84859927

爬虫示例: 将百度的图片下载到本地


'''
1.找到图片的url
2.发送请求获取响应
3.保存图片 (流媒体必须以二进制方式写入)
'''

import requests

def uploadBaidu():
    url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_redBlue_32fe2c69.png"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
    }
    res = requests.get(url=url,headers=headers)
    # 打开图片
    with open("baidu_log.png", "wb") as f:
        f.write(res.content)
    print("OK")

if __name__ == '__main__':
    uploadBaidu()

发送带参数的请求

  • 以下示例的响应结果,被百度拦截了

import requests

url = "https://www.baidu.com/s"
# 确定查询字符串参数字典
params = {
    'wd':"python"
}

# 设置header信息
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
}

# 带上请求头和请求参数
resp = requests.get(url, params=params, headers=headers)

print(resp.content.decode())

import requests

# 确定查询字符串参数字典
url = "https://www.baidu.com/s?wd=python"

# 设置header信息
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
}

# 带上请求头和请求参数
resp = requests.get(url, headers=headers)

print(resp.content.decode())
  • 模拟登录
# 百度翻译移动端
# -*-coding:utf-8-*-
import requests
import json
class BaiduTran(object):
    """百度翻译接口"""
    def __init__(self, f="zh", to="en"):
        self.url = "https://fanyi.baidu.com/basetrans"
        self.f = f
        self.to = to
        self.headers = {
            "User-Agent": "Mozilla / 5.0(Linux;Android 5.0;SM - G900P Build / LRX21T) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 69.0.3497.81 Mobile Safari / 537.36"
        }
        
    def get_resp(self, query):
        """
        获取翻译响应
        :param query:
        :return:
        """
        data = {
            "query":query,
            "from":self.f,
            "to":self.to
        }
        self.resp = requests.post(url=self.url,headers=self.headers, data=data)
        return self.resp

    def get_result(self):
        result = json.loads(self.resp.content.decode())["trans"][0]["dst"]
        return result


if __name__ == '__main__':
    tran = BaiduTran(f="zh", to="en")
    resp = tran.get_resp("今天天气真不错")
    # print(resp.content.decode())
    print(tran.get_result())

  • 使用IP代理
# -*-coding:utf-8-*-
import requests
import json
query = input("请输入要翻译的中文:")
url = "https://fanyi.baidu.com/basetrans"
headers = {
            "User-Agent": "Mozilla / 5.0(Linux;Android 5.0;SM - G900P Build / LRX21T) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 69.0.3497.81 Mobile Safari / 537.36"
        }
data = {
    'query': query,
    'from': 'zh',
    'to': 'en'
}
# 循环获取代理ip
while True:
    try:
        proxy_str = requests.get("代理ip池的url", timeout=3).text # 获取代理ip
        print("代理ip:%s" % proxy_str)
        proxies = {"https":proxy_str}
        resp = requests.post(url=url,data=data,headers=headers,proxies=proxies)
        break
    except:
        print("更换代理ip...")

print(resp.content.decode())
# 处理响应
print(json.loads(resp.content.decode())["trans"][0]["dst"])

  • 使用requests处理cookie有三种方法
- cookie字符串放在headers中

    headers = {
        "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
        "Cookie":" Pycharm-26c2d973=dbb9b300-2483-478f-9f5a-16ca4580177e; Hm_lvt_98b9d8c2fd6608d564bf2ac2ae642948=1512607763; Pycharm-26c2d974=f645329f-338e-486c-82c2-29e2a0205c74; _xsrf=2|d1a3d8ea|c5b07851cbce048bd5453846445de19d|1522379036"}

    requests.get(url,headers=headers)


- 把cookie字典放传给请求方法的cookies参数接收

    requests.get(url,headers=headers,cookies=cookie_dict)


- 使用requests提供的session模块

    session = requests.session()
    response = session.get(url,headers)

    session实例在请求了一个网站后,对方服务器设置在本地的cookie会保存在session中
    下一次再使用session请求对方服务器的时候,会带上前一次的cookie

  • 使用 cookies
import requests

url = "http://www.baidu.com"
#发送请求,获取resposne
response = requests.get(url)
print(type(response.cookies)) # <class 'requests.cookies.RequestsCookieJar'>

#使用方法从cookiejar中提取数据
cookies = requests.utils.dict_from_cookiejar(response.cookies)
print(cookies) # {'BDORZ': '27315'}

  • 证书错误的处理
import requests

url = "https://www.12306.cn/mormhweb/"

# requests.packages.urllib3.disable_warnings() # 不显示安全提示 

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

  • 超时响应
response = requests.get(url,timeout=3) # 发送请求之后,3秒内返回响应,否则会报错

标签:headers,Python,resp,get,爬虫,url,requests,self
From: https://www.cnblogs.com/qinganning/p/17036001.html

相关文章

  • 2023.1-09 python基础
    列表常用方法append增加一个元素a.append('aaaa')extend增加多个a.extend([1,2,3,4,5,6])index检索,个人理解类似于findprint(a.index("is"))inset指定位置插入......
  • Python笔记——列表一:列表简介(Python编程:从入门到实践)
    一、列表是什么列表:由一系列按特定顺序排列的元素组成(列表是有序集合)。表示:用方括号[]来表示,并用逗号来分隔其中的元素。访问:访问列表元素,可指出列表的名称,再指出......
  • python调试模块pdb:pdb基本用法(转)
    转载自:https://www.jianshu.com/p/fb5f791fcb18python调试:pdb基本用法(转)Edward_f0cusIP属地:台湾12016.01.0615:39:49字数767阅读73,778转自IBM开发......
  • python装饰器
    python装饰器--开放封闭原则知识点回顾#*args**kwargsdefwrapper(*args,**kwargs):#接受(形参)args=(1,2,3)kwargs={'a':1,'b':2}index(*args,**kwargs) #......
  • Python 中的生成器实现原理
    1.如何生成一个巨大的序列1.1需求描述要求生成一个包含很多元素的序列,假设:存储1个整数需要4个字节现在要创建一个包含1G个整数的序列,从0到1*1024*1024*......
  • 数据结构与算法Python版之北大慕课笔记(一)
    数据结构与算法Python版之北大慕课笔记(一)一、“变位词”判断问题1.解法一:逐字检查2.解法二:排序比较3.解法三:计数比较二、线性结构linearstructure1.四个......
  • python 每天一个知识点 第三天
    字典dict一,字典是什么?字典是Python中比较常用的数据结构,字典中每个成员是以“键:值”对的形式存放具有映射关系的数据。语法:字典以大括号“{}”包围的以“键:值”对......
  • Python 实现栈与队列
    栈栈(stack),有些地方称为堆栈,是一种容器,可存入数据元素、访问元素、删除元素,它的特点在于只能允许在容器的一端(称为栈顶端指标,英语:top)进行加入数据(英语:push)和输出数据(英语:po......
  • VsCode新建Java、SpringBoot、Python、JavaWeb项目的基本步骤
    新建Java项目选中正上方的搜索框,按下F1快捷键,输入createJava,即可出现这样的一个命令:选中这个:然后为新创建的项目选择一个合适的位置就好啦!新建SpringBoot项目选中......
  • 一步一步学爬虫(5)什么是Ajax
    (什么是Ajax)  Ajax,全称为AsynchronousJavaScriptandXML,即异步的JavaScript和XML。它不是一门编程语言,而是利用JavaScript在保证页面不被刷新、页面链接不改变......