《抓取免費(fèi)的代理IP供自己使用》【第二章】抓取快代理

首發(fā)于:https://mp.weixin.qq.com/s/O00A2FnYgcgThoEwdZTPaw

如何使用ip

既然我們找到了免費(fèi)的代理ip,我們要使用,怎么用呢,總不能一個(gè)個(gè)的復(fù)制吧,這不就太憨了嘛

我們使用爬蟲(chóng)技術(shù),把這些免費(fèi)的代理ip抓下來(lái)就是了

抓下來(lái)放進(jìn)數(shù)據(jù)庫(kù),后面用的時(shí)候直接使用程序提取數(shù)據(jù)庫(kù)中的代理ip,不就可以了嘛

思路還是簡(jiǎn)單清晰的把

下面就是開(kāi)始爬取各網(wǎng)站的代理ip......

抓取快代理

準(zhǔn)備

  • 網(wǎng)址:https://www.kuaidaili.com/free/
  • 系統(tǒng):windows
  • 瀏覽器:Google
  • 語(yǔ)言:python
  • 版本:3.x
  • 數(shù)據(jù)庫(kù):MongoDB

分析網(wǎng)址

先打開(kāi)網(wǎng)址看下:https://www.kuaidaili.com/free/

file

我們來(lái)點(diǎn)擊第二頁(yè)

file

看下網(wǎng)址


file

網(wǎng)址變化了,比較大可能是get請(qǐng)求,我們按下F12,打開(kāi)開(kāi)發(fā)者模式

我們?cè)冱c(diǎn)擊第一頁(yè)

file

在看下面的圖片,根據(jù)圖片里的步驟來(lái)

file

我們可以發(fā)現(xiàn)

file

這個(gè)就是我們想要的網(wǎng)址

代碼實(shí)現(xiàn)

import requests
from lxml import etree
import pymongo
import time
class get_ip:
    def __init__(self):
        _mongo = pymongo.MongoClient(host="127.0.0.1",port=27017)
        db = _mongo.IPS
        self.ip_table = db["ip_table"]

    def get_html(self,page):
        headers = {
            'Connection': 'keep-alive',
            'sec-ch-ua': '"Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"',
            'sec-ch-ua-mobile': '?0',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Sec-Fetch-Site': 'same-origin',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-User': '?1',
            'Sec-Fetch-Dest': 'document',
            'Referer': f'https://www.kuaidaili.com/free/inha/{page}/',
            'Accept-Language': 'zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7',
        }

        response = response = requests.get(f'https://www.kuaidaili.com/free/inha/{page}/', headers=headers)
        html = etree.HTML(response.text)
        trs = html.xpath('//div[@id="list"]//table/tbody/tr')
        print(trs)
        print(response)
        for tr in trs:
            ip = tr.xpath('td[@data-title="IP"]/text()')[0]
            port = tr.xpath('td[@data-title="PORT"]/text()')[0]
            print(f"{ip}:{port}")
            _find = self.ip_table.find_one({"ip":ip,"post":port})
            # 去重
            if not _find:
                print("ip不存在")
                self.ip_table.insert_one({"ip":ip,"post":port})
            else:
                print("ip已存在")
        
        
g = get_ip()
for i in range(1,9999):
    g.get_html(i)
    time.sleep(3)

關(guān)注我獲取更多內(nèi)容

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容