我想用漂亮的汤刮掉这个页面上的评论-
https://www.x....s.com/video_id/郊区
评论通过Javascript在点击时加载。评论是分页的,每个页面也会在点击时加载评论。我希望获取所有评论,对于每个评论,我想要得到的海报个人资料网址,评论,没有。喜欢的数量,不喜欢的数量和发布的时间(如页面所述)。
注释可以是字典列表。
我该怎么做呢?
发布于 2020-07-23 03:10:26
此脚本将打印在页面上找到的所有注释:
import json
import requests
from bs4 import BeautifulSoup
url = 'https://www.x......com/video_id/gggjggjj/'
video_id = url.rsplit('/', maxsplit=2)[-2].replace('video', '')
u = 'https://www.x......com/threads/video/ggggjggl/{video_id}/0/0'.format(video_id=video_id)
comments = requests.post(u, data={'load_all':1}).json()
for id_ in comments['posts']['ids']:
print(comments['posts']['posts'][id_]['date'])
print(comments['posts']['posts'][id_]['name'])
print(comments['posts']['posts'][id_]['url'])
print(BeautifulSoup(comments['posts']['posts'][id_]['message'], 'html.parser').get_text())
# ...etc.
print('-'*80)发布于 2020-07-23 01:15:38
这可以用硒来完成。Selenium模拟浏览器。根据您的喜好,您可以使用chrome驱动程序或Firefox驱动程序,后者是geckodriver。
这里有一个关于如何安装chrome webdriver的链接:
http://jonathansoma.com/lede/foundations-2018/classes/selenium/selenium-windows-install/
然后,在您的代码中,您将如何设置它:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# this part may change depending on where you installed the webdriver.
# You may have to define the path to the driver.
# For me my driver is in C:/bin so I do not need to define the path
chrome_options = Options()
# or '-start maximized' if you want the browser window to open
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
driver.get(your_url)
html = driver.page_source # downloads the html from the driverSelenium有几个函数,您可以使用它们来执行某些操作,比如单击页面上的元素。一旦找到带有selenium的元素,就可以使用.click()方法与该元素进行交互。如果有帮助,请让我知道
https://stackoverflow.com/questions/63039645
复制相似问题