初识python之 APP store排行榜 蜘蛛抓取(一)

直接上干货!!

采用python 2.7.5-windows

打开 http://www.apple.com/cn/itunes/charts/free-apps/ 

 

如上图可以见采用的是utf-8 编码  

 

经过一番思想斗争  编码如下 (拍砖别打脸)

#coding=utf-8
import urllib2    
import urllib    
import re    
import thread    
import time

  
    
#----------- APP store 排行榜 -----------    
class Spider_Model:    
        
    def __init__(self):    
        self.page = 1    
        self.pages = []    
        self.enable = False    
       
    def GetCon(self):    
        myUrl = "http://www.apple.com/cn/itunes/charts/free-apps/"    
        user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'   
        headers = { 'User-Agent' : user_agent }   
        req = urllib2.Request(myUrl, headers = headers)   
        myResponse = urllib2.urlopen(req)  
        myPage = myResponse.read()    
        #encode的作用是将unicode编码转换成其他编码的字符串    
        #decode的作用是将其他编码的字符串转换成unicode编码       
        print myPage
 
print ' '
myModel = Spider_Model()
myModel.GetCon()

  采集页面字符集 python文件字符集统一为utf-8 (贫蛋哥是认为没啥问题的)  

  打印输出结果:

                

       拿出杀手锏   www.baidu.com  

       找到原因:

        http://blog.csdn.net/lf8289/article/details/2465196

        http://www.crifan.com/unicodeencodeerror_gbk_codec_can_not_encode_character_in_position_illegal_multibyte_sequence/

  各种狂改中.......

        

#coding=gbk   编码修改为gbk
import urllib2    
import urllib    
import re    
import thread    
import time

  
    
#----------- APP store 排行榜 -----------    
class Spider_Model:    
        
    def __init__(self):    
        self.page = 1    
        self.pages = []    
        self.enable = False    
       
    def GetCon(self):    
        myUrl = "http://www.apple.com/cn/itunes/charts/free-apps/"    
        user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'   
        headers = { 'User-Agent' : user_agent }   
        req = urllib2.Request(myUrl, headers = headers)   
        myResponse = urllib2.urlopen(req)  
        myPage = myResponse.read()    
        #encode的作用是将unicode编码转换成其他编码的字符串    
        #decode的作用是将其他编码的字符串转换成unicode编码    
        unicodePage = myPage.decode('utf-8').encode('gbk','ignore') #采集页面编码为utf-8  转为 gbk (ignore来忽略非法的字符)

     print unicodePage
    print ' ' 
    myModel = Spider_Model()
    myModel.GetCon()

  运行结果:

            

转载于:https://www.cnblogs.com/etodream/p/3918264.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值