requests模块的高级用法

1.代理

  代理服务器,可以接受请求然后将其转发

1.匿名度

  1. 高匿:不知道你使用了代理,也不知道你的真实ip
  2. 匿名: 知道你使用了代理,但是不知道你的真实ip
  3. 透明:知道你使用了代理并且知道你的真实ip

2.类型

http
https

3.免费代理的网站

- http://www.goubanjia.com/
- 快代理
- 西祠代理
- http://http.zhiliandaili.cn/

构建代理池:

# 构建代理池
import requests
from lxml import etree

headers = {
    ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36‘,
    ‘Connection‘: "close"
}

#从代理精灵中提取代理ip
ip_url = ‘http://t.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=1&fa=0&fetch_key=&groupid=0&qty=4&time=1&pro=&city=&port=1&format=html&ss=5&css=&dt=1&specialTxt=3&specialJson=&usertype=2‘
page_text = requests.get(ip_url, headers=headers).text
tree = etree.HTML(page_text)
ip_list = tree.xpath(‘//body//text()‘)
print(ip_list)

# 爬取西祠代理的ip
url = "https://www.xicidaili.com/nn/%d"
proxy_list_http = []
proxy_list_https = []
for page in range(1, 2):
    new_url = format(url % page)
    page_text = requests.get(url=new_url, headers=headers).text
    tree = etree.HTML(page_text)
    tree_list = tree.xpath(‘//*[@id="ip_list"]//tr‘)[1:]
    for tr in tree_list:
        ip = tr.xpath("./td[2]/text()")[0]
        port = tr.xpath("./td[3]/text()")[0]
        ip_type = tr.xpath("./td[6]/text()")[0]
        if ip_type == "HTTP":
            dic = {
                ip_type: ip + ":" + port
            }
            proxy_list_http.append(dic)
        else:
            dic = {
                ip_type: ip + ":" + port
            }
            proxy_list_https.append(dic)
print(len(proxy_list_http), len(proxy_list_https))

# 检测
url = "https://www/sogou.com"
for ip in proxy_list_http:
    response = requests.get(url=url, headers=headers, proxies={‘https‘: ip})
    if response.status_code == "200":
        print("检测到可用的ip")

2.cookie的处理

  手动处理:jiangcookie封装到headers中

  自动处理:session对象,可以创建一个session对象,该对象的可以像requests一样进行请求发送,不同之处在于如果使用session进行请求发送的过程中产生了cookie,则cookie会被自动存储在session对象中。

#对雪球网中的新闻数据进行爬取https://xueqiu.com/
import requests
headers = {
    ‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36‘,
    ‘Cookie‘:‘aliyungf_tc=AQAAAAl2aA+kKgkAtxdwe3JmsY226Y+n; acw_tc=2760822915681668126047128e605abf3a5518432dc7f074b2c9cb26d0aa94; xq_a_token=75661393f1556aa7f900df4dc91059df49b83145; xq_r_token=29fe5e93ec0b24974bdd382ffb61d026d8350d7d; u=121568166816578; device_id=24700f9f1986800ab4fcc880530dd0ed‘
}
url = ‘https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=20349203&count=15&category=-1‘
page_text = requests.get(url=url,headers=headers).json()
print(page_text)
import requests
headers = {
    ‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36‘
}

#创建session对象
session = requests.Session()
session.get(‘https://xueqiu.com‘, headers=headers)

url = ‘https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=20349203&count=15&category=-1‘
page_text = session.get(url=url, headers=headers).json()
print(page_text)

3.模拟登陆

1.验证码识别

相关网站

  - 超级鹰:http://www.chaojiying.com/about.html

    • 注册:(用户中心身份)
    • 登陆:
      • 创建一个软件:
      • 下载实例代码

  - 打码兔

  - 云打码

识别古诗文网中的验证码

# 超级鹰的实例代码
import requests
from hashlib import md5
from lxml import etree

class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
        self.username = username
        password =  password.encode(‘utf8‘)
        self.password = md5(password).hexdigest()
        self.soft_id = soft_id
        self.base_params = {
            ‘user‘: self.username,
            ‘pass2‘: self.password,
            ‘softid‘: self.soft_id,
        }
        self.headers = {
            ‘Connection‘: ‘Keep-Alive‘,
            ‘User-Agent‘: ‘Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)‘,
        }

    def PostPic(self, im, codetype):
        """
        im: 图片字节
        codetype: 题目类型 参考 http://www.chaojiying.com/price.html
        """
        params = {
            ‘codetype‘: codetype,
        }
        params.update(self.base_params)
        files = {‘userfile‘: (‘ccc.jpg‘, im)}
        r = requests.post(‘http://upload.chaojiying.net/Upload/Processing.php‘, data=params, files=files, headers=self.headers)
        return r.json()

    def ReportError(self, im_id):
        """
        im_id:报错题目的图片ID
        """
        params = {
            ‘id‘: im_id,
        }
        params.update(self.base_params)
        r = requests.post(‘http://upload.chaojiying.net/Upload/ReportError.php‘, data=params, headers=self.headers)
        return r.json()

# 1.识别古诗文网站中的验证码
def tranformImgData(imgpath,t_type):
    chaojiying = Chaojiying_Client(‘15879478962‘, ‘15879478962‘, ‘901492‘)  # 超级鹰用户名,超级鹰用户名密码,软件id
    im = open(imgPath, ‘rb‘).read()
    return chaojiying.PostPic(im, t_type)[‘pic_str‘]
url = "https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx"
page_text = requests.get(url,headers=headers).text
tree = etree.HTML(page_text)
img_src = ‘https://so.gushiwen.org‘+tree.xpath(‘//*[@id="imgCode"]/@src‘)[0]
img_data = requests.get(img_src,headers=headers).content
with open("./code.jpg","wb") as fp:
    fp.write(img_data)
tranformImgData(‘./code.jpg‘,1004)

古诗文的模拟登陆

# 超级鹰的实例代码
import requests
from hashlib import md5
from lxml import etree

class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
        self.username = username
        password =  password.encode(‘utf8‘)
        self.password = md5(password).hexdigest()
        self.soft_id = soft_id
        self.base_params = {
            ‘user‘: self.username,
            ‘pass2‘: self.password,
            ‘softid‘: self.soft_id,
        }
        self.headers = {
            ‘Connection‘: ‘Keep-Alive‘,
            ‘User-Agent‘: ‘Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)‘,
        }

    def PostPic(self, im, codetype):
        """
        im: 图片字节
        codetype: 题目类型 参考 http://www.chaojiying.com/price.html
        """
        params = {
            ‘codetype‘: codetype,
        }
        params.update(self.base_params)
        files = {‘userfile‘: (‘ccc.jpg‘, im)}
        r = requests.post(‘http://upload.chaojiying.net/Upload/Processing.php‘, data=params, files=files, headers=self.headers)
        return r.json()

    def ReportError(self, im_id):
        """
        im_id:报错题目的图片ID
        """
        params = {
            ‘id‘: im_id,
        }
        params.update(self.base_params)
        r = requests.post(‘http://upload.chaojiying.net/Upload/ReportError.php‘, data=params, headers=self.headers)
        return r.json()

# 1.识别古诗文网站中的验证码
def tranformImgData(imgpath,t_type):
     chaojiying = Chaojiying_Client(‘[email protected]‘, ‘3089693229‘, ‘901492‘)
    im = open(imgPath, ‘rb‘).read()
    return chaojiying.PostPic(im, t_type)[‘pic_str‘]
url = "https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx"
page_text = requests.get(url,headers=headers).text
tree = etree.HTML(page_text)
img_src = ‘https://so.gushiwen.org‘+tree.xpath(‘//*[@id="imgCode"]/@src‘)[0]
img_data = requests.get(img_src,headers=headers).content
with open("./code.jpg","wb") as fp:
    fp.write(img_data)

# 模拟登陆
s = requests.Session()
url = ‘https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx‘
page_text = s.get(url,headers=headers).text
tree = etree.HTML(page_text)
img_src = ‘https://so.gushiwen.org‘+tree.xpath(‘//*[@id="imgCode"]/@src‘)[0]
img_data = s.get(img_src,headers=headers).content
with open("./code.jpg",‘wb‘) as fp:
    fp.write(img_data)
# 动态获取变化的请求参数
# - 动态变化的请求参数
#     - 通常情况下动态变化的请求参数都会被隐藏在前台页面源码中
__VIEWSTATE = tree.xpath(‘//*[@id="__VIEWSTATE"]/@value‘)[0]
__VIEWSTATEGENERATOR = tree.xpath(‘//*[@id="__VIEWSTATEGENERATOR"]/@value‘)[0]    

code_text = tranformImgData(‘./code.jpg‘,1004)

login_url = "https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx"
data = {
    "__VIEWSTATE": __VIEWSTATE,
    "__VIEWSTATEGENERATOR": __VIEWSTATEGENERATOR,
    "from": "http://so.gushiwen.org/user/collect.aspx",
    "email": "15879478962",
    "pwd": "15879478962",
    "code": code_text,
    "denglu": "登录",
}
page_text = s.post(url=login_url,headers=headers,data=data).text
with open("login.html","w",encoding="utf-8") as fp:
    fp.write(page_text)

原文地址:https://www.cnblogs.com/zangyue/p/12180488.html

时间: 2024-07-31 14:20:08

requests模块的高级用法的相关文章

requests模块的基本用法

requests 什么是requests模块 python中封装好的一个基于网络请求的模块 作用 用来模拟浏览器发送请求 环境安装 pip install requests 编码流程 指定 url 发起请求 获取响应数据 持久化存储 爬取搜狗首页的页面源码数据 #爬取搜狗首页的页面源码数据 import requests #1.指定url url = 'https://www.sogou.com/' #2.请求发送get:get返回值是一个响应对象 response = requests.get

re模块的高级用法

search 需求:匹配出文章阅读的次数 #coding=utf-8 import re ret = re.search(r"\d+", "阅读次数为 9999") ret.group() 运行结果: '9999' findall 需求:统计出python.c.c++相应文章阅读的次数 #coding=utf-8 import re ret = re.findall(r"\d+", "python = 9999, c = 7890, c

Python3网络爬虫实战-25、requests:高级用法

在前面一节我们了解了 Requests 的基本用法,如基本的 GET.POST 请求以及 Response 对象的用法,本节我们再来了解下 Requests 的一些高级用法,如文件上传,代理设置,Cookies 设置等等. 1. 文件上传 我们知道 Reqeuests 可以模拟提交一些数据,假如有的网站需要我们上传文件,我们同样可以利用它来上传,实现非常简单,实例如下: import requests files = {'file': open('favicon.ico', 'rb')} r =

python+requests——高级用法——处理cookie——重点

参考网址:https://www.cnblogs.com/xiaobaibailongma/p/12346091.html import requests url = 'http://www.baidu.com' resp = requests.get(url) print(resp.cookies) print('============================================================') for k,v in resp.cookies.item

Python requests模块学习

import requests 下面就可以使用神奇的requests模块了! 1.向网页发送数据 >>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']} >>> r = requests.get('http://httpbin.org/get', params=payload) >>> print(r.url) http://httpbin.org/get?key1=value1&a

请求库之requests模块

一 介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:requests库发送请求将网页内容下载下来以后,并不会执行js代码,这需要我们自己分析目标站点然后发起新的request请求 #安装:pip3 install requests #各种请求方式:常用的就是requests.get()和requests.post() >>> import requests >>&

requests模块之基础语法

基础语法 发送请求 使用 Requests 发送网络请求非常简单. 一开始要导入 Requests 模块:  >>> import requests  然后,尝试获取某个网页.本例子中,我们来获取 Github 的公共时间线:  >>> r = requests.get('https://api.github.com/events')  现在,我们有一个名为 r 的 Response 对象.我们可以从这个对象中获取所有我们想要的信息. Requests 简便的 API

再谈Newtonsoft.Json高级用法

上一篇Newtonsoft.Json高级用法发布以后收到挺多回复的,本篇将分享几点挺有用的知识点和最近项目中用到的一个新点进行说明,做为对上篇文章的补充. 阅读目录 动态改变属性序列化名称 枚举值序列化问题 全局设置 总结 回到顶部 动态改变属性序列化名称 "动态改变属性序列化名称"顾名思义:在不同场景下实体字段序列化后字段名称不同,比如有下面实体A,正常序列化后json为{"Id":"123"} public class A { public

细说 ASP.NET Cache 及其高级用法

许多做过程序性能优化的人,或者关注过程程序性能的人,应该都使用过各类缓存技术. 而我今天所说的Cache是专指ASP.NET的Cache,我们可以使用HttpRuntime.Cache访问到的那个Cache,而不是其它的缓存技术. 以前我在[我心目中的Asp.net核心对象] 这篇博客中简单地提过它,今天我打算为它写篇专题博客,专门来谈谈它,因为它实在是太重要了.在这篇博客中, 我不仅要介绍它的一些常见用法,还将介绍它的一些高级用法. 在上篇博客[在.net中读写config文件的各种方法] 的