2301_78158059 2023-07-23 00:16 采纳率: 75%
浏览 11
已结题

Python qq机器人

img


,请教各位一下,这个是什么意思,怎么弄啊!QQ机器人怎么运行

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-07-23 02:07
    关注
    • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7464739
    • 这篇博客你也可以参考下:[Python & 爬虫]爬取微博热搜,并应用在QQ机器人实现定时播报
    • 除此之外, 这篇博客: Python爬虫练习——爬取QQ音乐精彩评论(编码问题请高人指点!)中的 爬取QQ音乐精彩评论 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    • 代码如下(欢迎各位指正):

      import requests
      import re
      
      #获取网页源代码
      def get_code(url):
          headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0)"
                                   " Gecko/20100101 Firefox/73.0"}
          return requests.get(url, headers=headers).text
      
      
      #从原网页中提取songid
      song_url=input('请输入QQ音乐网页版网址(https://y.qq.com/n/yqq/song/x.html形式):')
      song_doc = get_code(song_url)
      id = re.findall('"songid":(.*?),"',song_doc)[0]
      name = re.findall('songname":"(.*?)"',song_doc)[0]
      tgt = int(input('请输入需要爬取的页数:'))
      #从js文件中提取评论
      list = []      #防止评论重复
      num = 0
      for number in range(1,tgt):
          comment_url = "https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg?g_tk" \
                        "=5381&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset" \
                        "=GB2312&notice=0&platform=yqq&needNewCode=0&cid=205360772&reqtype" \
                        "=2&biztype=1&topid=%s&cmd=6&needmusiccrit=0&pagenum=%d&pagesize=" \
                        "15&ct=24&cv=10101010"%(id,number)
          comment_doc = get_code(comment_url)
          targets = re.findall('"rootcommentcontent":"(.*?)"',comment_doc)
          for each in targets:
              each = re.sub(r"([em](.*?)[/em])|(\\n)|(\\)|[|]",'',each)
              if each in list:
                  pass
              else:
                  file = open('热门评论\\'+name+'.txt', 'a')
                  try:
                      num += 1
                      num_st = str(num)
                      list.append(each)
                      each = re.sub('\[|\]', '', each)
                      file.write('编号'+num_st+' '+each+'\n')
                      print('第'+num_st+'个评论已爬取!')
                      file.close()
                  except Exception as err:
                      print(err)
          print('爬取成功!请查看当先文件夹下歌名文本!')
      

      有时候会出现这样的错误——

      'gbk' codec can't encode character '\xb4' in position 23:
       illegal multibyte sequence
      

      看来应该是编码的问题,但未找到解决方案…

      =========================================================

      更新:

      经过指点与修改(2处)——

      第一处:

      file = open('热门评论\\'+name+'.txt', 'a')
      

      改后:

      file = open('热门评论\\'+name+'.txt', 'a',encoding="utf-8")
      

      另一处修改:

      return requests.get(url, headers=headers).text
      

      改后:

      return requests.get(url, headers=headers).content.decode("utf-8")
      

      尝试了几次之后,并没有出现报错!初步鉴定合格…

    • 您还可以看一下 刘宇宙老师的Python爬虫视频教程:教你爬取QQ音乐数据(实战处理+数据可视化)课程中的 自然语言分词与词频统计——数据爬取小节, 巩固相关知识点
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 7月31日
  • 已采纳回答 7月23日
  • 创建了问题 7月23日