• day25-登录和代理IP


    day25-登录和代理IP

    1.request登录反扒过程

    1)用谷歌浏览器打开需要爬取的网站,人工完成登录
    2)获取这个网站登录后的cookie信息:
    检查 -> Network -> All -> 网站域名 -> Headers -> RequestHeader -> cookie
    3)在headers对应的字典中添加键值对: ‘cookie’: 获取到的cookie信息

    headers = {
        'cookie': '...',
        'user-agent': '...'
    } 
    response = requests.get('https://www.zhihu.com/', headers=headers)
    
    print(response.text)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    selenium获取cookie
    1. 打开需要做自动登录的网站

    2. 提供足够长的时间让人工在这个页面中完成登录(登录后一定要保证b对应的窗口出现登录信息)

    3. 获取登录后的cookie信息保存到本地文件中(建议保存一个json)

    b = Chrome()
    b.get('https://www.taobao.com')
    input('登录完成:')
    cookies = b.get_cookies()
    
    with open('files/taobao.json', 'w', encoding='utf-8') as f:
        f.write(dumps(cookies))
    
    b.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    selenium使用cookie
    1. 打开需要爬取的网站
    2. 从cookie文件中获取cookie信息并且添加到浏览器对象中
    3. 重新打开网页
    b = Chrome()
    b.get('https://www.taobao.com')
    with open('files/taobao.json', encoding='utf-8') as f:
        cookies = loads(f.read())
    
    for x in cookies:
        b.add_cookie(x)
        
    b.get('https://www.taobao.com')
    
    input('end:')
    b.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    requests使用代理IP
    headers = {
        'user-agent': '...'
    }
    
    # 创建代理对象
    # proxies = {
    #     'https': 'http://183.165.224.25:4554',
    #     'http': 'http://183.165.224.25:4554'
    # }//此处IP使用时间很短,可能已过期
    proxies = {
        'https': '183.165.224.25:4554',
        'http': '183.165.224.25:4554'
    }
    # 发送请求的时候使用代理
    response = requests.get('https://www.maoyan.com/', headers=headers, proxies=proxies)
    # 解决乱码问题
    response.encoding = 'utf-8'
    print(response.text)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    代理ip的实际用法
    import requests
    import time
    from bs4 import BeautifulSoup
    
    
    def get_ip():
        """
        获取代理ip,如果获取失败过2秒再重新获取
        :return: 获取到的ip地址
        """
        while True:
            response = requests.get('...')#此处为获取代理IP的网址,可网上搜索
            result = response.text
            if result[0] == '{':
                print('ip获取失败')
                time.sleep(2)
            else:
                return result
    
    
    def get_net_data():
        url = 'https://www.maoyan.com/'
        headers = {
            'user-agent': '...'
        }
    
        # 使用代理ip发送请求,如果代理失败,重新获取新的ip重新再发送请求
        while True:
            ip = get_ip()
            print(ip)
            proxy = {
                'https': ip
            }
            response = requests.get(url, headers=headers, proxies=proxy)
            response.encoding = 'utf-8'
            print(response.text)
    
            soup = BeautifulSoup(response.text, 'lxml')
            movies_div = soup.select('.movie-list .movie-item')
            if len(movies_div) == 0:
                continue
            else:
                print('爬虫成功!做后续的解析操作')
                break
    
    
    if __name__ == '__main__':
        get_net_data()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    海尔智家:“超预期”成为“新常态”
    【ESP32蓝牙通信】gatt_client 和 gatt_server 调试
    大厂面试题:ReentrantLock 与 synchronized异同点对比
    HDFS dfs指令
    微慕积分商城插件
    《你的第一本哲学书》- 是否存在外部世界
    刷题数据结构实现类
    哈希表题目:唯一摩尔斯密码词
    LitCTF2023 - Reverse方向 全WP
    十. 实战——云服务器
  • 原文地址:https://blog.csdn.net/qq_40752621/article/details/125452033