python 爬虫获取代理Ip

本文介绍如何在Python3.5环境下,利用BeautifulSoup库从免费代理IP网站获取IP和端口,通过访问特定网址测试其可用性,并最终返回可用代理IP的字典列表。提供了一个基础版本及多线程版本的实现。
摘要由CSDN通过智能技术生成

要点:(环境Python3.5,额外库bs4)

1.从免费代理ip获取ip和端口号  http://www.xicidaili.com/nn/

2.使用代理

import urllib.request
proxy_support = urllib.request.ProxyHandler({'http': 'ip:port'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)

3.urlopen测试ip能否使用   http://ip.chinaz.com/getip.aspx

4.返回包含可以使用的ip和端口组成的字典的list

贴代码:

from urllib.request import urlopen
import re
import requests
from bs4 import BeautifulSoup as bs
from urllib import request
import socket

#init timeout = 3
socket.setdefaulttimeout(3)

#request the xiciURL and get the response
def request_to_get(url):
	hearder = {
		"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
		"Accept-Encoding":"gzip, deflate",
		"A
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值