簡體   English   中英

如何通過 BeautifulSoup 獲取已發布的博客文本?

[英]How to get posted blog text through BeautifulSoup?

我制作了一個 Telegram 機器人並使用插件來獲取博客(博主)的文本。 但我收到以下錯誤。

代碼:

import requests
import re
from bs4 import BeautifulSoup
from pyrogram import Client, filters, enums

@Client.on_message(filters.command("mzl"))
async def mzlyc(client, message):
    await client.send_chat_action(message.chat.id, action=enums.ChatAction.TYPING)
    mzr = message.text.split(None, 1)[1]
    mizoly = mzr.replace(" ", "+") if len(message.command) > 2 else mzr
    mizl = f"https://www.mizolyric.com/search?q={mizoly}&m=1"
    zol = requests.get(mizl).text
    soup = BeautifulSoup(zol, 'html.parser')
    item = soup.select_one("div[id^='post-body-']").text
    if not item:
        return await client.send_message(message.chat.id, \
            text="I lyrics duh hi ka zawng hmu zo lo.",  \
            reply_to_message_id=message.id)
    itms = item.replace("Continue Reading", " ") if "Continue Reading" in item else item
    await client.send_message(message.chat.id, text=f"{itms}", reply_to_message_id=message.id)

此代碼適用於此模板: https://www.templatemark.com/2017/10/news16-magazine-blogger-template.html; 但不適用於模板https://www.templatemark.com/2020/05/msd-responsive-grid-style-blogger-template.html

錯誤:

Traceback (most recent call last):
2022-08-21T19:21:40.630002+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.10/site-packages/pyrogram/dispatcher.py", line 240, in handler_worker
2022-08-21T19:21:40.630002+00:00 app[worker.1]:     await handler.callback(self.client, *args)
2022-08-21T19:21:40.630002+00:00 app[worker.1]:   File "/app/plugins/Tools4/mzlyrics.py", line 17, in mzlyc
2022-08-21T19:21:40.630003+00:00 app[worker.1]:     item = soup.select_one("div[id^='post-body-']").text
2022-08-21T19:21:40.630003+00:00 app[worker.1]: AttributeError: 'NoneType' object has no attribute 'text'
    

如何解決這個問題?

嘗試以下更改:

zol = requests.get(mizl).content
soup = BeautifulSoup(zol.decode('utf-8','ignore'), 'html.parser')

這是編碼 unicode 字符的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM