漂亮的汤python。从html获取完整信息
我想通过BeautifulSoup通过电报获得我的帖子的浏览量。例如,我想从我的频道帖子956中获取: 它会打印:漂亮的汤python。从html获取完整信息,python,python-3.x,parsing,beautifulsoup,telegram,Python,Python 3.x,Parsing,Beautifulsoup,Telegram,我想通过BeautifulSoup通过电报获得我的帖子的浏览量。例如,我想从我的频道帖子956中获取: 它会打印: [<div class="tgme_page_widget" id="widget"> <script async="" data-telegram-post="dayygesstt/956" data-width="100%" src="https://telegram.org/js/telegram- widget.js?4"></script&g
[<div class="tgme_page_widget" id="widget">
<script async="" data-telegram-post="dayygesstt/956" data-width="100%" src="https://telegram.org/js/telegram-
widget.js?4"></script>
</div>]
我尝试了不同的东西,但我不能得到更多的信息。请帮帮我,我做错了什么?如何正确获取信息?我认为您需要定义与BeautifulSoup一起使用的解析器,以便它正确解析HTML,因此这一行
soup=BeautifulSoup(html, )
需要这样做
soup=BeautifulSoup(html, 'html.parser')
您可以在脚本中使用加载iframe的URL。然后你只需要一个小部件而不需要它。为此,获取原始URL并附加查询字符串embed=1
import requests
from bs4 import BeautifulSoup
url = 'https://t.me/dayygesstt/956?embed=1'
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
views = soup.find("span", {"class": "tgme_widget_message_views"})
print(views.text)
它不起作用,因为div元素的内容是用javascript动态加载的。谢谢,我该怎么办?
soup=BeautifulSoup(html, 'html.parser')
import requests
from bs4 import BeautifulSoup
url = 'https://t.me/dayygesstt/956?embed=1'
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
views = soup.find("span", {"class": "tgme_widget_message_views"})
print(views.text)