首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何__scrape__通过Javascript加载的页面外数据

如何__scrape__通过Javascript加载的页面外数据
EN

Stack Overflow用户
提问于 2020-07-23 01:09:48
回答 2查看 70关注 0票数 2

我想用漂亮的汤刮掉这个页面上的评论-

https://www.x....s.com/video_id/郊区

评论通过Javascript在点击时加载。评论是分页的,每个页面也会在点击时加载评论。我希望获取所有评论,对于每个评论,我想要得到的海报个人资料网址,评论,没有。喜欢的数量,不喜欢的数量和发布的时间(如页面所述)。

注释可以是字典列表。

我该怎么做呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-23 03:10:26

此脚本将打印在页面上找到的所有注释:

代码语言:javascript
复制
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)
票数 2
EN

Stack Overflow用户

发布于 2020-07-23 01:15:38

这可以用硒来完成。Selenium模拟浏览器。根据您的喜好,您可以使用chrome驱动程序或Firefox驱动程序,后者是geckodriver。

这里有一个关于如何安装chrome webdriver的链接:

http://jonathansoma.com/lede/foundations-2018/classes/selenium/selenium-windows-install/

然后,在您的代码中,您将如何设置它:

代码语言:javascript
复制
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 driver

Selenium有几个函数,您可以使用它们来执行某些操作,比如单击页面上的元素。一旦找到带有selenium的元素,就可以使用.click()方法与该元素进行交互。如果有帮助,请让我知道

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63039645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档