一直对爬虫这块蛮感兴趣的,所以花了点时间看了看,写了个小脚本

代码可能有点乱,毕竟Python小白,勿喷……

嗯,话不多说,放码出来

 # -*- coding: UTF-8 -*-
import re
import requests headers = {"User-Agent" : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;"} url = "http://www.xicidaili.com/nn/" context = requests.get(url,headers = headers) #ip和端口
# pattern = re.compile("<td>\d+\.\d+\.\d+\.\d+</td>\s+<td>\d+</td>")
pattern = re.compile("<td>\d+\.\d+\.\d+\.\d+</td>\s+<td>\d+</td>\s+<td>\s+<.*?</a>\s+</td>\s+<.*?</td>\s+<td>[A-Z]{2,6}</td>") # re.sub字串替换
pat = re.compile('::<.*?::<.*?:') #例:123.135.62.217:8118::<ahref="/2018-01-24/shandong">山东泰安</a>::<tdclass="country">高匿:HTTPS
#匹配规则:?::<.*?: content = pattern.findall(context.text)
for item in content:
item = item.replace("<td>","").replace("</td>","").replace("\n",":").replace(" ","")
item = pat.sub("__",item)
with open("ip.txt","a") as f:
f.write(item+"\n") #ip数
i = 0
#页面数
j = 1 #pass ip使用次数
#防止过多使用同一个ip被封,虽用的代理ip,还是感觉不太好,勿喷
#当然,ip和页面一对一又显得浪费
#所以加了这个机制
x = 0
f = open("ip.txt")
lines = f.readlines() #数组的长度,Python应该是字典
# print len(lines) while i<len(lines):
url = "http://www.xicidaili.com/nn/"+str(j) #ip类型判断
if re.findall("HTTPS",lines[i].replace("\n","")):
ip = "https://"+lines[i].replace("\n","").replace("__HTTPS","")
proxies = {
"https":ip
}
elif re.findall("HTTP",lines[i].replace("\n","")):
ip = "http://"+lines[i].replace("\n","").replace("__HTTP","")
proxies = {
"http":ip
}
else:
print "代理ip获取错误..."
exit() #判断ip是否可用
try:
response = requests.get(url,headers = headers,proxies = proxies)
except:
print "第"+str(i)+"次失败"
i = i+1
else:
context = pattern.findall(response.text)
#可用ip保存,存到ip_pass.txt
if x>8:
with open("ip_pass.txt","a") as f:
f.write(lines[i])
i = i+1
x = 1
print "第"+str(i)+"次成功"
print "."
print "."
print "."
else:
x = x+1
#保存页面信息
for item in context:
item = item.replace("<td>","").replace("</td>","").replace("\n",":").replace(" ","")
item = pat.sub("__",item)
with open("ips.txt","a") as f:
f.write(item+"\n")
print "第"+str(j)+"页爬取成功"
j = j+1
print "success"

Python爬虫——西刺的更多相关文章

  1. python爬西刺代理

    爬IP代码 import requests import re import dauk from bs4 import BeautifulSoup import time def daili(): p ...

  2. python scrapy 爬取西刺代理ip(一基础篇)(ubuntu环境下) -赖大大

    第一步:环境搭建 1.python2 或 python3 2.用pip安装下载scrapy框架 具体就自行百度了,主要内容不是在这. 第二步:创建scrapy(简单介绍) 1.Creating a p ...

  3. python+scrapy 爬取西刺代理ip(一)

    转自:https://www.cnblogs.com/lyc642983907/p/10739577.html 第一步:环境搭建 1.python2 或 python3 2.用pip安装下载scrap ...

  4. 手把手教你使用Python爬取西刺代理数据(下篇)

    /1 前言/ 前几天小编发布了手把手教你使用Python爬取西次代理数据(上篇),木有赶上车的小伙伴,可以戳进去看看.今天小编带大家进行网页结构的分析以及网页数据的提取,具体步骤如下. /2 首页分析 ...

  5. Python四线程爬取西刺代理

    import requests from bs4 import BeautifulSoup import lxml import telnetlib #验证代理的可用性 import pymysql. ...

  6. python爬虫成长之路(二):抓取代理IP并多线程验证

    上回说到,突破反爬虫限制的方法之一就是多用几个代理IP,但前提是我们得拥有有效的代理IP,下面我们来介绍抓取代理IP并多线程快速验证其有效性的过程. 一.抓取代理IP 提供免费代理IP的网站还挺多的, ...

  7. Python爬虫代理池

    爬虫代理IP池 在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代理IP,从而保证爬虫快速稳定的运行,当然在公司做的东西不能开源出来 ...

  8. python爬虫实战(一)——实时获取代理ip

    在爬虫学习的过程中,维护一个自己的代理池是非常重要的. 详情看代码: 1.运行环境 python3.x,需求库:bs4,requests 2.实时抓取西刺-国内高匿代理中前3页的代理ip(可根据需求自 ...

  9. 爬取西刺ip代理池

    好久没更新博客啦~,今天来更新一篇利用爬虫爬取西刺的代理池的小代码 先说下需求,我们都是用python写一段小代码去爬取自己所需要的信息,这是可取的,但是,有一些网站呢,对我们的网络爬虫做了一些限制, ...

随机推荐

  1. shell 构建脚本基础

    bash -v test.sh 启用 verbose 调试模式 bash -n test.sh  启用语法检查调试模式 bash -x test.sh  遍历脚本执行过程 一.基础参数 1.shell ...

  2. 自制操作系统Antz(10)——实现shell(上)

    我已经规范了系统代码风格,类似于按照linux分包,把各部分功能区分开了 Antz系统更新地址 Linux内核源码分析地址 Github项目地址 在之前的任务中,我们已经通过直接操作显卡驱动完成了简单 ...

  3. snmp模拟器snmpsid使用

    snmpsim使用 安装 pip install snmpsim 简单使用 生成snmpwalk文件: snmpwalk -v2c -c 'password' -ObentU 218.200.x.15 ...

  4. BZOJ-3208|记忆化搜索-花神的秒题计划Ⅰ

    背景[backboard]: Memphis等一群蒟蒻出题中,花神凑过来秒题-- 描述[discribe]: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目. 我 ...

  5. 论文笔记:AdaScale: Towards real-time video object detection using adaptive scalingAdaScale

    AdaScale: Towards real-time video object detection using adaptive scaling 2019-02-18 16:14:17 Paper: ...

  6. VS Code插件配置

    常用 VS Code 插件: Auto Import 有了这个插件,就不需要再手动引入文件了.如果是基于组件的项目,直接输入组件名插件会自动处理 imported. ** Add jsdoc comm ...

  7. 数据库连接超时:“The last packet successfully received from the server was xxx milliseconds ago”

    产生的原因:应用方的数据库连接有效期时间,大于数据库自己设置的有效期. 解决方案: 一.修改druid配置(如果使用druid的话) spring.datasource.druid.validatio ...

  8. 开启BBR

    BBR 目的是要尽量跑满带宽, 并且尽量不要有排队的情况, 效果并不比速锐差Linux kernel 4.9+ 已支持 tcp_bbr 下面简单讲述基于KVM架构VPS如何开启附:OpenVZ 架构V ...

  9. 接口测试工具Jmeter

    一.安装Jmeter 1.安装JDK ①下载jdk,到官网下载jdk,地址:http://jmeter.apache.org/download_jmeter.cgi ② 安装jdk(Oracle官网下 ...

  10. sql字符串根据日期产生日期+自增长标志

    select Convert(varchar(6), '20180614 20:19:04', 12) + SubString('0000' + Convert(varchar(6), 1 + 1), ...