日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/10 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 初识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排行榜 蜘蛛抓取(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。