繁体   English   中英

注释在网页上可见,但是BeautifulSoup返回的html对象不包含注释部分

[英]Comments are visible on the webpage, but the html object returned by BeautifulSoup did not contain the comment parts

我尝试使用其URL链接从网页中提取评论的文本内容,然后使用BeautifulSoup进行抓取。 当我单击URL链接时,注释的内容在页面上可见,但是BeautifulSoup返回的HTML对象不包含这些标记和文本。

我将BeautifulSoup与'html.parser'一起使用进行网络抓取。 我成功地提取了给定网页中视频的喜欢/观看/评论的数量,但是评论部分的信息未包含在HTML文件中。 我使用的浏览器是Chrome,系统是Ubuntu 18.04.1 LTS。

这是我使用的代码(在python中):

from urllib.request import urlopen
from bs4 import BeautifulSoup
import os

webpage_link = "https://www.airvuz.com/video/Majestic-Beast-Nanuk?id=59b2a56141ab4823e61ea901"

try:
    page = urlopen(webpage_link)
except urllib.error.HTTPError as err:  # webpage cannot be found
    print("ERROR! %s" %(webpage_link))

soup = BeautifulSoup(page, 'html.parser')

预期的结果是,汤对象包含所有在网页上可见的内容,特别是评论的文本内容(例如“不在那儿,我对白熊的生活方式非常满意。这要归功于此类纪录片的提供者。”和“哇...好极了...”); 但是,我在汤对象中找不到相应的节点。 任何帮助,将不胜感激!

注释是由JavasSript通过ajax请求生成的。 您可以发送相同的请求并从json响应中获取评论。 您可以使用检查工具中的“网络”标签找到请求。

from urllib.request import urlopen
from bs4 import BeautifulSoup, Comment
import json
webpage_link = "https://www.airvuz.com/api/comments/video/59b2a56141ab4823e61ea901?page=1&limit=20"
page = urlopen(webpage_link).read()
comments_json=data = json.loads(page)
for comment_info in comments_json['data']:
    print(comment_info['comment'].strip()) 

输出量

Not being there I enjoyed a lot seeing the life style of white bear. Thanks to the provider for  such documentary.
WOOOW... amazing...
I've been photographing polar bears for years, but to see this footage from a drones perspective was epic! Well done and congratz on the Nominee! Well deserved.
You are da man Florian!
Absolutely outstanding!
This is incredible
jaw dropping
This is wow amazing, love it.
So cool! Did the bears react to the drone at all?
Congratulations! It's awesome! I am watching in tears....
Awesome!
perfect video awesome
It is very, very beautiful !!! Sincere congratulations
Made my day, exquisite, thank you
Wow
Super!
Marvelous!
Man this is incredible!
Material is good, but  edi is bad. This history about  beer's family...
Muy bueno!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM