溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用python爬蟲(chóng)論壇

發(fā)布時(shí)間:2020-11-12 09:26:08 來(lái)源:億速云 閱讀:169 作者:小新 欄目:編程語(yǔ)言

這篇文章給大家分享的是有關(guān)怎么使用python爬蟲(chóng)論壇的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。

庫(kù):requests,re,selenium,time

具體步驟:

一、搜索貼吧

準(zhǔn)備一個(gè)自己想要爬取的內(nèi)容

二、顯示貼吧首頁(yè)的帖子

要提取發(fā)帖人的名字和帖子主題,選擇用正則表達(dá)式來(lái)實(shí)現(xiàn),具體代碼如下:

titles = re.findall('<a rel="noreferrer" href="/p/(\w+)" title="(.*?)"', res.text)
authors = re.findall('title="主題作者: (.*?)"', res.text)

三、查看某一個(gè)帖子

通過(guò)觀察網(wǎng)頁(yè)源碼,可以用正則表達(dá)式匹配到這部分回復(fù),然后對(duì)于這些回復(fù),也要進(jìn)行處理后再顯示出來(lái)。

comment_list2 = re.findall('post_bubble_middle_inner">(.*?)<div', res.text, re.DOTALL)
comment = comment_list2[num].replace('<br>', ' ').replace('</div>', ' ')

具體源碼如下:

import requests
 import time
  import re
 from selenium import webdriver
 
 headers = {
 "user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/"
  "53.0.2785.89 Safari/537.36"
 }
  
 
  class TieBa:
  def __init__(self, name, url):
  self.name = name # 貼吧名
 self.url = url # 貼吧首頁(yè)的url
 
  # 顯示首頁(yè)上的帖子作者和標(biāo)題
 def get_homepage(self):
  print("\n\t\t\t\t\t\t{}吧".format(self.name))
 res = requests.get(self.url, headers=headers)
 titles = re.findall('<a rel="noreferrer" href="/p/(\w+)" title="(.*?)"', res.text)
 authors = re.findall('title="主題作者: (.*?)"', res.text)
  for title, author in zip(titles, authors):
  print(str(titles.index(title) + 1) + "--" + author + ":" + title[1])
  print("\n****************************")
  n = int(input("請(qǐng)輸入您想要查看的帖子序號(hào):"))
  print("****************************\n")
  self.get_page("http://tieba.baidu.com/p/" + titles[n - 1][0])
  
  # 顯示帖子的具體內(nèi)容
  def get_page(self, page_url):
  res = requests.get(page_url, headers=headers)
  author_list = re.findall('p_author_name j_user_card.*?>(.*?)<', res.text)
  comment_list = re.findall('content clearfix">(.*?)</div>', res.text, re.DOTALL)
  if len(comment_list) == 0:
  comment_list = re.findall('d_post_content j_d_post_content ">(.*?)</div>', res.text, re.DOTALL)
 
 # 帶回復(fù)框的單獨(dú)考慮
  comment_list2 = re.findall('post_bubble_middle_inner">(.*?)<div', res.text, re.DOTALL)
  num = 0
 
  # 處理圖片和表情,顯示對(duì)應(yīng)的鏈接
 for author, comment in zip(author_list, comment_list):
  print(author.strip(), end=" : ")
  comment = comment.strip().replace('<br>', ' ')
  if "<div" in comment: # 解析有評(píng)論框的評(píng)論
  comment = comment_list2[num].replace('<br>', ' ').replace('</div>', ' ')
  num += 1
  if "src" in comment: # 處理圖片信息
  src_list = re.findall('src="(.*?)"', comment)
 pattern_list = re.findall('(<img.*?>)', comment)
  for s, p in zip(src_list, pattern_list):
  comment = comment.replace(p, ' ' + s)
  if "href" in comment: # 處理表情信息
  pattern_list = re.findall('(<a .*? >)', comment)
 for p in pattern_list:
  comment = comment.replace(p, ' ').replace("</a>", '')
 print(comment)
  
  while True:
 x = int(input("\n輸入1查看下一頁(yè),輸入0回到首頁(yè),輸入-1退出程序:"))
  if x == -1:
  exit()
  if x == 0:
  self.get_homepage()
  if x == 1:
  next_page = re.findall('<a href="(.*?)">下一頁(yè)', res.text)
  if len(next_page)>0:
  self.get_page("http://tieba.baidu.com" + next_page[0])
  else:
  print("已經(jīng)是最后一頁(yè)了!")
   
  # 利用selenium模擬瀏覽器查找貼吧
  def search():
  chrome_options = webdriver.ChromeOptions()
  chrome_options.add_argument('--headless')
  browser = webdriver.Chrome(chrome_options=chrome_options)
 
  browser.get("https://tieba.baidu.com/")
  name = input("請(qǐng)輸入想要進(jìn)入的貼吧名字:")
  browser.find_element_by_xpath('//*[@id="wd1"]').send_keys(name)
  browser.find_element_by_xpath('//*[@id="tb_header_search_form"]/span[1]/a').click()
 url = browser.current_url
 if "tieba.baidu.com/f?ie=utf-8" in url: # 搜索的貼吧存在
  print("正在進(jìn)入{}吧,請(qǐng)稍等...".format(name))
  time.sleep(2)
  t = TieBa(name, url)
  t.get_homepage()
  else:
  print("沒(méi)有找到{}吧\n".format(name))
  main()
  
  
 def main():
 x = int(input("輸入1搜索進(jìn)入貼吧,輸入-1退出程序:"))
  if x == 1:
  search()
  else:
 exit()
 
 if __name__ == '__main__':
main()

感謝各位的閱讀!關(guān)于怎么使用python爬蟲(chóng)論壇就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI