鱼C论坛

 找回密码
 立即注册
查看: 3892|回复: 2

关于爬取IP的问题,求大神指导

[复制链接]
发表于 2017-11-28 14:56:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#coding=utf-8
import re
import urllib.request
import urllib.parse
import os
url='http://www.kuaidaili.com/ops/' #http://www.xicidaili.com/  http://www.proxy360.cn/default.aspx
headers={'Accept':'text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8',
         'Accept-Encoding':'gzip,deflate',
         'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
         'Host':'fs.xicidaili.com',
         'Cookie':'Hm_lvt_0cf76c77469e965d2957f0553e6ecf59=1511441689,1511784134,1511785760; Hm_lpvt_0cf76c77469e965d2957f0553e6ecf59=1511785760',
         'Connection':'keep-alive',
         'User-Agent':'Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/57.0'  
    }
headers=urllib.parse.urlencode(headers).encode('utf8')
rep=urllib.request.Request(url,headers)
reponse=urllib.request.urlopen(rep)
html=reponse.read().decode(encoding='UTF-8')
#print (html)
p=re.compile(r'((1[0-9][0-9]\.)|(2[0-4][0-9]\.)|(25[0-5]\.)|([1-9][0-9]\.)|([0-9]\.)){3}((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))')
ip_list=p.search(html)
print(ip_list)
print(ip_list.group())

为什么我这只抓取了一个IP?
7.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-11-30 19:19:55 | 显示全部楼层
  1. #coding=utf-8
  2. import re
  3. import urllib.request
  4. import urllib.parse
  5. import os
  6. from urllib.error import URLError,HTTPError
  7. url='http://www.kuaidaili.com/ops/' #http://www.xicidaili.com/  http://www.proxy360.cn/default.aspx  http://www.kuaidaili.com/ops/
  8. headers={'Accept':'text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8',
  9.          'Accept-Encoding':'gzip,deflate',
  10.          'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
  11.          'Host':'fs.xicidaili.com',
  12.          'Cookie':'Hm_lvt_0cf76c77469e965d2957f0553e6ecf59=1511441689,1511784134,1511785760; Hm_lpvt_0cf76c77469e965d2957f0553e6ecf59=1511785760',
  13.          'Connection':'keep-alive',
  14.          'User-Agent':'Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/57.0'
  15. }
  16. headers=urllib.parse.urlencode(headers).encode('utf8')
  17. rep=urllib.request.Request(url,headers)
  18. reponse=urllib.request.urlopen(rep)
  19. html=reponse.read().decode(encoding='UTF-8')
  20. reg=r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'
  21. ip_list=re.findall(reg,html)
  22. print(ip_list)
  23. f=open('ip.txt','a')
  24. f.write(str(ip_list)+'\n')
  25. f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-30 19:21:09 | 显示全部楼层
search只返回一个值
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 09:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表