欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python爬蟲(chóng)爬取微博評(píng)論案例詳解

 更新時(shí)間:2019年03月27日 10:20:28   作者:china-mogul  
這篇文章主要介紹了python爬蟲(chóng)爬取微博評(píng)論,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前幾天,楊超越編程大賽火了,大家都在報(bào)名參加,而我也是其中的一員。

在我們的項(xiàng)目中,我負(fù)責(zé)的是數(shù)據(jù)爬取這塊,我主要是把對(duì)于楊超越 的每一條評(píng)論的相關(guān)信息。

數(shù)據(jù)格式:{"name":評(píng)論人姓名,"comment_time":評(píng)論時(shí)間,"comment_info":評(píng)論內(nèi)容,"comment_url":評(píng)論人的主頁(yè)}

以上就是我們需要的信息。

爬蟲(chóng)前的分析:

以上是楊超越的微博主頁(yè),這是我們首先需要獲取到的內(nèi)容。

因?yàn)槲覀冃枰鹊竭@個(gè)主頁(yè)內(nèi)這些微博詳情頁(yè) 的鏈接,但是我們向下刷新,會(huì)發(fā)現(xiàn)微博的主頁(yè)信息是ajax動(dòng)態(tài)加載出來(lái)的,

這張圖片就是我們向下刷新獲取到 的新的鏈接,這個(gè)就是我們需要獲取到的信息頁(yè)面信息。

接下來(lái) 就是獲取詳情頁(yè)面的信息,詳情頁(yè)中含有評(píng)論的相關(guān)信息,通過(guò)向下刷新,我們也會(huì)發(fā)現(xiàn),相關(guān)的評(píng)論信息也是通過(guò)ajax加載出來(lái)的 ,

ok,以上就是我們針對(duì)整個(gè)流程大致的一個(gè)分析過(guò)程。

具體操作流程:

我們首相將主頁(yè)獲取完成以后,我們就會(huì)發(fā)現(xiàn),其中 的內(nèi)容帶有相關(guān)的反爬措施,獲取到的源碼中的信息含有很多的轉(zhuǎn)義符“\”,并且其中的相關(guān)“<”和“>”是通過(guò)html的語(yǔ)言直接編寫(xiě)的,這樣會(huì)導(dǎo)致我們的頁(yè)面解析出現(xiàn)一定的問(wèn)題,我們可以用replace方法直接將這些轉(zhuǎn)義符全部去掉,然后我們就可以對(duì)這個(gè)頁(yè)面進(jìn)行正則處理,同時(shí)我也嘗試過(guò)用其他的解析方法,但是其中遇到了很多 的問(wèn)題,所以我就不過(guò)多的介紹了。

當(dāng)我們獲取到了每一篇微博的鏈接以后,還需要獲取一個(gè)很關(guān)鍵的值  id   ,這個(gè)值有什么用呢,其主要的作用就是在評(píng)論頁(yè)面的ajax頁(yè)面的拼接地址上需要使用到。接下來(lái)就是需要尋找出我們找到的這兩個(gè)ajax的url有什么特點(diǎn)或者是規(guī)律:

當(dāng)我們從這些ajax中找到規(guī)律以后,不難發(fā)現(xiàn),這個(gè)爬蟲(chóng)差不多大功告成了。

下面我就展示一下我的代碼:

 注意:請(qǐng)?jiān)趆eaders中添加自己的cookie

# -*- coding: utf-8 -*-
# Created : 2018/8/26 18:33
# author :GuoLi
 
import requests
import json
import time
from lxml import etree
import html
import re
from bs4 import BeautifulSoup
 
 
class Weibospider:
  def __init__(self):
    # 獲取首頁(yè)的相關(guān)信息:
    self.start_url = 'https://weibo.com/u/5644764907?page=1&is_all=1'
 
    self.headers = {
      "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
      "accept-encoding": "gzip, deflate, br",
      "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
      "cache-control": "max-age=0",
      "cookie": 使用自己本機(jī)的cookie,
      "referer": "https://www.weibo.com/u/5644764907?topnav=1&wvr=6&topsug=1",
      "upgrade-insecure-requests": "1",
      "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
    }
    self.proxy = {
      'HTTP': 'HTTP://180.125.70.78:9999',
      'HTTP': 'HTTP://117.90.4.230:9999',
      'HTTP': 'HTTP://111.77.196.229:9999',
      'HTTP': 'HTTP://111.177.183.57:9999',
      'HTTP': 'HTTP://123.55.98.146:9999',
    }
 
  def parse_home_url(self, url): # 處理解析首頁(yè)面的詳細(xì)信息(不包括兩個(gè)通過(guò)ajax獲取到的頁(yè)面)
    res = requests.get(url, headers=self.headers)
    response = res.content.decode().replace("\\", "")
    # every_url = re.compile('target="_blank" href="(/\d+/\w+\?from=\w+&wvr=6&mod=weibotime)" rel="external nofollow" ', re.S).findall(response)
    every_id = re.compile('name=(\d+)', re.S).findall(response) # 獲取次級(jí)頁(yè)面需要的id
    home_url = []
    for id in every_id:
      base_url = 'https://weibo.com/aj/v6/comment/big?ajwvr=6&id={}&from=singleWeiBo'
      url = base_url.format(id)
      home_url.append(url)
    return home_url
 
  def parse_comment_info(self, url): # 爬取直接發(fā)表評(píng)論的人的相關(guān)信息(name,info,time,info_url)
    res = requests.get(url, headers=self.headers)
    response = res.json()
    count = response['data']['count']
    html = etree.HTML(response['data']['html'])
    name = html.xpath("http://div[@class='list_li S_line1 clearfix']/div[@class='WB_face W_fl']/a/img/@alt") # 評(píng)論人的姓名
    info = html.xpath("http://div[@node-type='replywrap']/div[@class='WB_text']/text()") # 評(píng)論信息
    info = "".join(info).replace(" ", "").split("\n")
    info.pop(0)
    comment_time = html.xpath("http://div[@class='WB_from S_txt2']/text()") # 評(píng)論時(shí)間
    name_url = html.xpath("http://div[@class='WB_face W_fl']/a/@href") # 評(píng)論人的url
    name_url = ["https:" + i for i in name_url]
    comment_info_list = []
    for i in range(len(name)):
      item = {}
      item["name"] = name[i] # 存儲(chǔ)評(píng)論人的網(wǎng)名
      item["comment_info"] = info[i] # 存儲(chǔ)評(píng)論的信息
      item["comment_time"] = comment_time[i] # 存儲(chǔ)評(píng)論時(shí)間
      item["comment_url"] = name_url[i] # 存儲(chǔ)評(píng)論人的相關(guān)主頁(yè)
      comment_info_list.append(item)
    return count, comment_info_list
 
  def write_file(self, path_name, content_list):
    for content in content_list:
      with open(path_name, "a", encoding="UTF-8") as f:
        f.write(json.dumps(content, ensure_ascii=False))
        f.write("\n")
 
  def run(self):
    start_url = 'https://weibo.com/u/5644764907?page={}&is_all=1'
    start_ajax_url1 = 'https://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain=100406&is_all=1&page={0}&pagebar=0&pl_name=Pl_Official_MyProfileFeed__20&id=1004065644764907&script_uri=/u/5644764907&pre_page={0}'
    start_ajax_url2 = 'https://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain=100406&is_all=1&page={0}&pagebar=1&pl_name=Pl_Official_MyProfileFeed__20&id=1004065644764907&script_uri=/u/5644764907&pre_page={0}'
    for i in range(12): # 微博共有12頁(yè)
      home_url = self.parse_home_url(start_url.format(i + 1)) # 獲取每一頁(yè)的微博
      ajax_url1 = self.parse_home_url(start_ajax_url1.format(i + 1)) # ajax加載頁(yè)面的微博
      ajax_url2 = self.parse_home_url(start_ajax_url2.format(i + 1)) # ajax第二頁(yè)加載頁(yè)面的微博
      all_url = home_url + ajax_url1 + ajax_url2
      for j in range(len(all_url)):
        print(all_url[j])
        path_name = "第{}條微博相關(guān)評(píng)論.txt".format(i * 45 + j + 1)
        all_count, comment_info_list = self.parse_comment_info(all_url[j])
        self.write_file(path_name, comment_info_list)
        for num in range(1, 10000):
          if num * 15 < int(all_count) + 15:
            comment_url = all_url[j] + "&page={}".format(num + 1)
            print(comment_url)
            try:
              count, comment_info_list = self.parse_comment_info(comment_url)
              self.write_file(path_name, comment_info_list)
            except Exception as e:
              print("Error:", e)
              time.sleep(60)
              count, comment_info_list = self.parse_comment_info(comment_url)
              self.write_file(path_name, comment_info_list)
            del count
            time.sleep(0.2)
 
        print("第{}微博信息獲取完成!".format(i * 45 + j + 1))
 
 
if __name__ == '__main__':
  weibo = Weibospider()
  weibo.run()
 

以上所述是小編給大家介紹的python爬蟲(chóng)爬取微博評(píng)論詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論