初识python之 APP store排行榜 蜘蛛抓取(一)
直接上干貨!!
采用python 2.7.5-windows
打開(kāi)?http://www.apple.com/cn/itunes/charts/free-apps/?
?
如上圖可以見(jiàn)采用的是utf-8 編碼 ?
?
經(jīng)過(guò)一番思想斗爭(zhēng) ?編碼如下 (拍磚別打臉)
#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編碼轉(zhuǎn)換成其他編碼的字符串 #decode的作用是將其他編碼的字符串轉(zhuǎn)換成unicode編碼 print myPageprint ' ' myModel = Spider_Model() myModel.GetCon()采集頁(yè)面字符集 python文件字符集統(tǒng)一為utf-8 (貧蛋哥是認(rèn)為沒(méi)啥問(wèn)題的) ?
打印輸出結(jié)果:
? ? ? ??
? ? ? ?拿出殺手锏 ? 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編碼轉(zhuǎn)換成其他編碼的字符串 #decode的作用是將其他編碼的字符串轉(zhuǎn)換成unicode編碼 unicodePage = myPage.decode('utf-8').encode('gbk','ignore') #采集頁(yè)面編碼為utf-8 轉(zhuǎn)為 gbk (ignore來(lái)忽略非法的字符)
print unicodePage print ' '
myModel = Spider_Model()
myModel.GetCon()
運(yùn)行結(jié)果:
? ? ??
轉(zhuǎn)載于:https://www.cnblogs.com/etodream/p/3918264.html
總結(jié)
以上是生活随笔為你收集整理的初识python之 APP store排行榜 蜘蛛抓取(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用 Python 构建电影推荐系统
- 下一篇: 基于python实现的电影推荐系统