python获取代理IP

利用requests库获取代理,用Beautiful库解析网页筛选ip

# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
from threading import Thread

headers = {‘user-agent‘: ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0‘}

#定义获取IP函数
def get_ip():
   #写入txt
    write_ip = open(‘get_ip.txt‘, ‘w‘)
for page in range(1, 10):
        url = ‘http://www.xicidaili.com/nn/%s‘ % page
        r = requests.get(url, headers=headers,timeout=5)

        # 用beautifulsoup库解析网页
        soup = BeautifulSoup(r.content, ‘lxml‘)
        trs = soup.find(‘table‘, id=‘ip_list‘).find_all(‘tr‘)

        for tr in trs[1:]:
            tds = tr.find_all(‘td‘)
            ip = tds[1].text.strip()
            port = tds[2].text.strip()
            write_ip.write(‘%s\n‘%(ip+‘:‘+port))
    write_ip.close()    print(‘done‘)

get_ip()
时间: 2024-08-07 20:33:19

python获取代理IP的相关文章

分享一个获取代理ip的python函数

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #coding:utf-8 from bs4 import BeautifulSoup import requests import random def getproxyip(): headers = { 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encodi

python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客

python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客 undefined Python多线程抓取代理服务器 | Linux运维笔记 undefined java如果模拟请求重启路由器(网络爬虫常用),还有java如何下载图片 - baidu_nod的专栏 - 博客频道 - CSDN.NET undefined [多线程数据采集]使用Jsoup抓取数据+破解屏蔽ip访问. - MiniBu - 开源中国社区 undefined 单linux服务器同时拨多条AD

python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性)

python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性) 目录 随机User-Agent 获取代理ip 检测代理ip可用性 随机User-Agent fake_useragent库,伪装请求头 from fake_useragent import UserAgent ua = UserAgent() # ie浏览器的user agent print(ua.ie) # opera浏览器 print(ua.opera) # chrome浏览器 print(ua.chro

获取代理IP地址

今天在开源中国上看到有个有写了个小程序,用来获取代理IP地址.用的beautifulsoup. 自己动手用正则重写了一下. #!/usr/bin/python import requests import re pattern=re.compile(r'(\d+)\D(\d+)\D(\d+)\D(\d+)\D(\d+)') headers={'Host':"www.ip-adress.com", 'User-Agent':"Mozilla/5.0 (Windows NT 6.

Python3.x:获取代理ip

Python3.x:获取代理ip 获取代理ip,代码: # python3 # 国内高匿代理IP网站:http://www.xicidaili.com/nn/ # 爬取首页代理IP地址 from bs4 import BeautifulSoup import requests import random # 获取首页IP列表 def get_ip_list(url, headers): web_data = requests.get(url, headers=headers) soup = Be

获取代理IP地址(BeautifulSoup)

前天用正则的方式获取网站的代理IP数据,今天为了学习BeautifulSoup,用BeautifulSoup实现了一下. 1 #!/usr/bin/python 2 3 import requests 4 from bs4 import BeautifulSoup 5 6 7 headers={'Host':"www.ip-adress.com", 8 'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gec

python检测代理IP可否翻越GFW

那堵墙着实可恨!身处IT这个圈子,经常需要用gg查资料(你也可以用来访问1024,^_^...).当然,你也可以用百度.其实也不是我不爱用百度,是有缘由的,且听我细细道来.有一次闲得蛋疼,想看看会不会有人抄袭我的博客(尽管博客学得不咋地),于是百度了一下,结果是惊人的.我发现我自己写的博客,即使是拿整个标题去搜索,往往搜不到,搜到的是一堆爬虫爬去的结果.具体是哪些,这里就不说了,各自可以拿自己的博客试一下.以前总是手工收集几个IP用一段时间,失效了以后再重新收集几个,如此反复,烦!于是,想着写个

kylin类库之获取代理IP

1 namespace Kylin.GetHttpIp 2 { 3 public class kylinIp 4 { 5 ///爬虫获取网站的高匿代理IP 6 ///目前使用的网站有: 7 ///http://www.xdaili.cn/freeproxy 8 ///http://www.xicidaili.com/nn/ 9 ///http://www.goubanjia.com/free/gngn/index.shtml 10 /// 11 12 ///第一个网站可以抓包:http://ww

python 获取自身ip

原文 见过很多获取服务器本地IP的代码,个人觉得都不是很好,例如以下这些 不推荐:靠猜测去获取本地IP方法 #!/usr/bin/env python # -*- coding: utf-8 -*- import socket import fcntl import struct def get_ip_address(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcnt