首页 > 解决方案 > 如何在当前 Firefox/Mozilla 实例中从新窗口中抓取?

问题描述

我可以在 Firefox/Mozilla 中打开一个新选项卡,抓取内容,然后将大量文本写入文件,就像这样。

from bs4 import BeautifulSoup
import requests
import urllib.request
import webbrowser
url = 'https://console.cloud.google.com/'
webbrowser.open_new_tab(url)

r=requests.get(url)
data = r.text

print(data)

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

file = open('C:\\path_here\\test.txt', 'w')
file.write(data)
file.close()

问题是我只从网站中获取一小部分文本到文本文件中。如何将站点中的所有文本转储到文件中?我猜某些内容必须是动态生成的,否则所有内容都会转储到文本文件中。我可以看出该站点中有一个 JavaScript 组件,但我不确定如何利用它。

标签: pythonpython-3.x

解决方案


如何使用硒:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://console.cloud.google.com/')
browser.page_source

推荐阅读