初识python之 APP store排行榜 蜘蛛抓取(一)
生活随笔
收集整理的這篇文章主要介紹了
初识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 myPageprint ' ' 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
總結
以上是生活随笔為你收集整理的初识python之 APP store排行榜 蜘蛛抓取(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用 Python 构建电影推荐系统
- 下一篇: container_of深入理解