Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
漂亮的汤python。从html获取完整信息_Python_Python 3.x_Parsing_Beautifulsoup_Telegram - Fatal编程技术网

漂亮的汤python。从html获取完整信息

漂亮的汤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

我想通过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>
</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)