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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

微指数爬虫

發布時間:2024/6/21 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 微指数爬虫 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

做微指數的爬蟲時,遇到點難題,去網上查些資料,發現有點錯誤;

特此經過研究,有了正確的采集方法。

1、采集界面,搜索關鍵字后頁面跳轉到指數界面,顯示搜索的指數。

2、fiddler抓包定位采集數據源,加載方式為json

3、獲取json數據;

分析數據的加載方式,發現數據是在定位請求頭中的referer地址進行返回數據的,

將重定向的地址解析后知道wid和wname是我們需要首先獲得的,其實就是我們請求的地址。

4、代碼驗證,這些步驟也是網上可以搜索到的步驟

a、獲取wid和對應的搜索關鍵字

b、拼接json數據的url

c、請求數據

5、用此方法驗證的代碼,結果無論搜索什么只顯示歡樂頌的指數,說明該方法是不能實現的

#coding=utf-8  
import sys  
reload(sys)  
sys.setdefaultencoding( "utf-8" )  
import requests  
import urllib  
  
class xl():  
    def pc(self,name):  
        url_name=urllib.quote(name)  
        headers={  
'Host': 'data.weibo.com',  
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0',  
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',  
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',  
'Accept-Encoding': 'gzip, deflate',  
'Content-Type': 'application/x-www-form-urlencoded',  
'X-Requested-With': 'XMLHttpRequest',  
'Referer': 'http://data.weibo.com/index/hotword?wname='+url_name,  
'Cookie': 'UOR=www.baidu.com,data.weibo.com,www.baidu.com; SINAGLOBAL=1213237876483.9214.1464074185942; ULV=1464183246396:2:2:2:3463179069239.6826.1464183246393:1464074185944; DATA=usrmdinst_12; _s_tentry=www.baidu.com; Apache=3463179069239.6826.1464183246393; WBStore=8ca40a3ef06ad7b2|undefined; PHPSESSID=3mn5oie7g3cm954prqan14hbg5',  
'Connection': 'keep-alive'  
}  
        r=requests.get("http://data.weibo.com/index/ajax/getchartdata?month=default&__rnd=1464188164238",headers=headers)  
        return r.text  
  
x=xl()  
print x.pc("歡樂頌") 

6、糾正方法,重新進入搜索頁;在紅方框中重新搜索關鍵字進行刷新

7、實際的數據加載過程為下面的四個步驟,“歡樂頌”的實際的請求url為

http://data.weibo.com/index/ajax/getchartdata?wid=1091324230349&sdate=2017-05-23&edate=2017-06-22&__rnd=1498202175662

8、獲取url中的日期和wid即可以拿到數據,不需要cookie

#coding=utf-8

import requests
import urllib
import cProfile

def search_name(name):
    url_format = "http://data.weibo.com/index/ajax/hotword?word={}&flag=nolike&_t=0"
    cookie_header = {
                     "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36",
                     "Referer":"http://data.weibo.com/index?sudaref=www.google.com"
                   }

    urlname = urllib.quote(name)
    first_requests = url_format.format(urlname)
    codes = requests.get(first_requests,headers=cookie_header).json()
    ids = codes["data"]["id"]

    header = {
        "Connection":"keep-alive",
        "Accept-Encoding": "gzip, deflate, sdch",
        "Accept": "*/*",
        "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36",
        "Accept-Language": "zh-CN,zh;q=0.8",
        "Referer": "http://data.weibo.com/index/hotword?wid={}&wname={}".format(ids,urlname),
        "Content-Type": "application/x-www-form-urlencoded",
        "Host":"data.weibo.com"
    }

    #獲取日期
    date_url = "http://data.weibo.com/index/ajax/getdate?month=1&__rnd=1498190033389"
    dc = requests.get(date_url,headers=header).json()
    edate,sdate = dc["edate"],dc["sdate"]

    #數據返回
    codes = requests.get("http://data.weibo.com/index/ajax/getchartdata?wid={}&sdate={}&edate={}"
                         .format(ids,sdate,edate),headers=header).json()
    return codes


if __name__ == "__main__":
    cProfile.run('search_name("天津")')
    #print search_name("天涯")

9、如有錯誤還請指正

總結

以上是生活随笔為你收集整理的微指数爬虫的全部內容,希望文章能夠幫你解決所遇到的問題。

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