我的第一个小爬虫程序-python

1 篇文章 0 订阅
1 篇文章 0 订阅
  • 爬什么?爬代理服务器网站的服务器 端口 代理种类 所在地区 更新日期 今日评分 总的评分 可用 速度测评信息,这样的网页有七八个,好在网址明名很规则

    爬这里的东西

具体说就是爬很多的这样的html代码里的信息

 <span class="tbBottomLine" style="width:140px;">
     58.222.254.11
 </span>
 <span class="tbBottomLine" style="width:50px;">
         3128
 </span>
 <span class="tbBottomLine " style="width:70px;">
     高匿
 </span>
 <span class="tbBottomLine " style="width:70px;">
     中国
 </span>
 <span class="tbBottomLine " style="width:80px;">
     09月05日
 </span>
 <span class="tbBottomLine " style="width:80px;">
     3.44(70票)
 </span>
 <span class="tbBottomLine " style="width:60px;">
     3.44
 </span>
 <span class="tbBottomLine " style="width:30px;">
     14天
 </span>
  • 怎么爬?利用python里的这个库urllib2,当然也要用正则,这里不得说我的正则好复杂(实在是想不到怎么简化)
import re
import urllib2

orgi=r'http://www.proxy360.cn/Region/'
proxywebs=['China','Brazil','America','Taiwan','Japan','Thailand','Vietnam','bahrein']

def get_Proxy_Info():
    f=open('proxy.txt','w+')
    p=re.compile("<span[^>]*>[\s]*(\S+?)[\s]*</span>[\s]*<span[^>]*>[\s]*(\S+?)[\s]*</span>[\s]*<span[^>]*>[\s]*(\S+?)[\s]*</span>[\s]*<span[^>]*>[\s]*(\S+?)[\s]*</span>[\s]*<span[^>]*>[\s]*(\S+?)[\s]*</span>[\s]*<span[^>]*>[\s]*(\S+?)[\s]*</span>[\s]*<span[^>]*>[\s]*(\S+?)[\s]*</span>[\s]*<span[^>]*>[\s]*(\S+?)[\s]*</span>")
     '''
    服务器 端口 代理种类 所在地区 更新日期 今日评分 总的评分 可用 速度测评 '''
    for s in proxywebs:
        target=orgi+s;
        req=urllib2.urlopen(target)
        mystr=req.read()
        tmp=p.findall(mystr)
        for t in tmp:
            f.write('\t'.join(t))
            f.write('\n')
            f.flush()
    f.close()

if __name__=="__main__":
    get_Proxy_Info()
  • 原理,或流程
    超简单,获取指定网页的源代码,返回类型是字符串,我们利用正则表达式匹配到我们想抓去的部分,利用分组进一步提取出来,然后保存或者之后的数据分析,显然这一步我没有做

  • 注意事项

    • 网页的编码,一般是UTF8,万一不是呢?我写的时候,保存的时候乱码了,后来才知道要注意编码格式

    • 正则的编写,这个考功底,其实也不然,你也可以很死的写,虽然我不推荐,但是我一开始只会这样写

<span class="tbBottomLine" style="width:140px;">\r\n(.+?)\r\n</span>
<span class="tbBottomLine" style="width:50px;">\r\n(.+?)\r\n</span>
<span class="tbBottomLine " style="width:70px;">\r\n(.+?)\r\n</span>
<span class="tbBottomLine " style="width:70px;">\r\n(.+?)\r\n</span>
<span class="tbBottomLine " style="width:80px;">\r\n(.+?)\r\n</span>
<span class="tbBottomLine " style="width:80px;">\r\n(.+?)\r\n</span>
<span class="tbBottomLine " style="width:60px;">\r\n(.+?)\r\n</span>
<span class="tbBottomLine " style="width:30px;">\r\n(.+?)\r\n</span>

很糟糕,但是能爬下来-_-#


  • 结果
    这里写图片描述

注:环境:python2.7 WingIDE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值