網絡中獲取大數據時使用代理ip的好處

網絡中獲取大數據時使用代理ip的好處


代理ip的益處

代理服務器英文全稱是(Proxy Server),也叫做代理IP,其功能就是代理網絡用戶去取得網絡信息。形象的說:它是網絡信息的中轉站。代理服務器就好像一個大的Cache,這樣就能顯著提高瀏覽速度和效率。更重要的是:Proxy Server(代理服務器)是Internet鏈路級網關所提供的一種重要的安全功能。

代理服務器的好處

1、防止攻擊:隱藏自己的真實地址信息,還可隱藏自己的IP,防止被黑客攻擊。

2、突破限制:代理服務器還可以突破網絡限制。比如局域網對上網用戶的端口,目的網站,協議,遊戲,即時通訊軟件等的限制,都可以突破這些限制。(有些公司禁止員工玩遊戲或上某些網站,這種情況就可以試試看代理服務器)。

3、掩藏身份:代理服務器知識是黑客基本功,黑客的很多活動都是通過代理服務器, 比如掃描、刺探,對局域網內機器進行滲透,黑客一般攻擊的時候都是中轉了很多級跳板,才攻擊目標機器。隱藏了身份,保證了自己的安全。

現如今很多網站給我提供不少的代理IP使用網站,但是數據太多需要我們手動複製太過於麻煩,則我們可以批量獲取下來,然後進行使用。


使用python批量獲取代碼如下:

import requests

import pandas as pd

import random

import time

import json

import re

from bs4 import BeautifulSoup as BS

def req_res(url):

# 隨機請求頭

USER_AGENT = [

"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",

"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",

"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",

"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",

"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",

"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",

"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",

]

user_agent = random.choice(USER_AGENT)

headers = {'User-Agent': user_agent}

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

return response # 返回響應

# 解析ip

def parse_res(response, items):

soup = BS(response,'lxml')

# 先取出所需數據位置

tr_list = soup.select('#list > table > tbody > tr')[1:]

# 遍歷列表取出所需數據

for tr in tr_list:

ip_add = tr.select('td')[0].text # 獲取ip地址

port = tr.select('td')[1].text # 獲取端口

server_add = tr.select('td')[4].text # 服務器地址

anonymous_type = tr.select('td')[2].text #否匿名

http_type = tr.select('td')[3].text # http類型

time_pr = tr.select('td')[6].text # 驗證時間

# 數據錄入字典

item = {

'IP地址': ip_add,

'端口': port,

'服務器地址': server_add,

'匿名信息': anonymous_type,

'類型': http_type,

'驗證時間': time_pr

}

items.append(item)

return items

def main():

url = 'https://www.kuaidaili.com/free/inha/'

start_page = int(input('\\n請輸入起始頁碼:'))

end_page = int(input('\\n請輸入結束頁碼:'))

items = []

# 循環爬取指定頁面

for page in range(start_page, end_page + 1):

print('\\n第{}頁開始爬取'.format(page))

response = req_res(url + str(page) + '/')

items = parse_res(response, items)

time.sleep(1.5)

print('\\n第{}頁結束爬取'.format(page))

# 以CSV格式保存數據

df = pd.DataFrame(items)

df.to_csv('IP_data.csv', index=False, sep=',', encoding='utf-8-sig')

print('*' * 30)

print('全部爬取結束')

print(items)

for dicc in items:

print(re.sub(r'[{}[\\]"]*','',json.dumps(dicc,ensure_ascii=False)))

if __name__ == '__main__':

main()


分享到:


相關文章: