怎么使用python爬虫论坛

2023-05-29,

这篇文章给大家分享的是有关怎么使用python爬虫论坛的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

库:requests,re,selenium,time

具体步骤:

一、搜索贴吧

准备一个自己想要爬取的内容

二、显示贴吧首页的帖子

要提取发帖人的名字和帖子主题,选择用正则表达式来实现,具体代码如下:

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

三、查看某一个帖子

通过观察网页源码,可以用正则表达式匹配到这部分回复,然后对于这些回复,也要进行处理后再显示出来。

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 # 贴吧首页的url
 
  # 显示首页上的帖子作者和标题
 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("请输入您想要查看的帖子序号:"))
  print("****************************\n")
  self.get_page("http://tieba.baidu.com/p/" + titles[n - 1][0])
  
  # 显示帖子的具体内容
  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)
 
 # 带回复框的单独考虑
  comment_list2 = re.findall('post_bubble_middle_inner">(.*?)<div', res.text, re.DOTALL)
  num = 0
 
  # 处理图片和表情,显示对应的链接
 for author, comment in zip(author_list, comment_list):
  print(author.strip(), end=" : ")
  comment = comment.strip().replace('<br>', ' ')
  if "<div" in comment: # 解析有评论框的评论
  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查看下一页,输入0回到首页,输入-1退出程序:"))
  if x == -1:
  exit()
  if x == 0:
  self.get_homepage()
  if x == 1:
  next_page = re.findall('<a href="(.*?)">下一页', res.text)
  if len(next_page)>0:
  self.get_page("http://tieba.baidu.com" + next_page[0])
  else:
  print("已经是最后一页了!")
   
  # 利用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("请输入想要进入的贴吧名字:")
  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("正在进入{}吧,请稍等...".format(name))
  time.sleep(2)
  t = TieBa(name, url)
  t.get_homepage()
  else:
  print("没有找到{}吧\n".format(name))
  main()
  
  
 def main():
 x = int(input("输入1搜索进入贴吧,输入-1退出程序:"))
  if x == 1:
  search()
  else:
 exit()
 
 if __name__ == '__main__':
main()

感谢各位的阅读!关于怎么使用python爬虫论坛就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!

《怎么使用python爬虫论坛.doc》

下载本文的Word格式文档,以方便收藏与打印。