首發(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)容