简体   繁体   English

如何从我当前在机器上的浏览器中打开的网页中获取文本/html?

[英]How can I get text/html from a web page I currently have open in a browser on my machine?

I want to do something like this:我想做这样的事情:

from lxml import html
import requests

page = requests.get('https://a-website.com/')

But instead of passing a defined hard-coded url, I would like to get the page that I currently have open in my web browser.但是,我不想传递定义的硬编码 url,而是想获取当前在 Web 浏览器中打开的页面。 ie page = requests.get(whateverisopeninmychrome) .page = requests.get(whateverisopeninmychrome) For what it's worth, its the text contents of a div that I am specifically looking for.对于它的价值,它是我专门寻找的 div 的文本内容。

Is there any way to do this or is it even possible?有什么办法可以做到这一点,或者甚至有可能吗? I could not find any other information about pulling the html/contents from a browser that is currently open on your machine.我找不到有关从您计算机上当前打开的浏览器中提取 html/内容的任何其他信息。

No, you can't like this.不,你不能喜欢这个。 One way is to open Developer Console on Google Chrome or Firefox with your website open and use javascript syntax like this:一种方法是在Google ChromeFirefox上打开开发者控制台,同时打开您的网站并使用如下 javascript 语法:

For searching by className , it will return a list and you will have to get one element:要按 className搜索,它将返回一个列表,您必须获得一个元素:

var html = document.getElementsByClassName('htmlClassHere')[0].innerText

For searching by id , it will return the element and you can use it like this:对于按id搜索,它将返回元素,您可以像这样使用它:

var html = document.getElementsById('htmlIdHere').innerText

暂无
暂无

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

相关问题 如何使用 Python 在 Web 浏览器中打开网站? - How can I open a website in my web browser using Python? 如何使用 Python 3 在我的 web 浏览器中打开网站? - How can I open a website in my web browser using Python 3? 在 Python 3.7/Django 3 中,如何在 web 浏览器中获取页面的渲染 HTML? - In Python 3.7/Django 3, how can I get the rendered HTML of a page in a web browser? 登录后如何使用python在Web浏览器中打开html文件? - How to open an html file in web browser using python after I have logged in? 当我尝试打开网站页面时,我有一个'NoReverseMatch'错误,我该如何修复它? - When I try open web-site page i have an a 'NoReverseMatch' error, how I can fix it? 如何在浏览器中显示我的 HTML 文件? 我已经成功连接服务器和客户端 - How can I display my HTML file in browser? I have already successfully connected the server and client 我已经解析了网页中的所有“ href”链接。 我只想用浏览器打开其中一个。 我怎样才能做到这一点? - I have parsed all “href” links from a webpage. I want to open only one of them with my browser. How can I do that? 如何使用 Python 从网页中获取数据? - How do I can get the data from a web page with Python? 如何从可以进行html校正和js脚本编写的浏览器中获取html内容? - How can I get html content from a browser that can do the html correction and js scripting? 如何使用 lxml 从这个 HTML 片段中获取文本? - How can I get the text from this HTML snippet using lxml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM