python中怎么获取js的输出值_使用Python中的BeautifulSoup在HTML源代码中获取JS var值(Get JS var value in HTML source using Be...
使用Python中的BeautifulSoup在HTML源代码中获取JS var值(Get JS var value in HTML source using BeautifulSoup in Python)我正在尝试使用BeautifulSoup从HTML源代码中获取JavaScript var值。例如我有:[other code]var my = 'hello';var name = 'hi'
使用Python中的BeautifulSoup在HTML源代码中获取JS var值(Get JS var value in HTML source using BeautifulSoup in Python)
我正在尝试使用BeautifulSoup从HTML源代码中获取JavaScript var值。
例如我有:
[other code]
var my = 'hello';
var name = 'hi';
var is = 'halo';
[other code]
我想要一些东西来返回Python中var“my”的值
我怎样才能做到这一点?
I'm trying to get a JavaScript var value from an HTML source code using BeautifulSoup.
For example I have:
[other code]
var my = 'hello';
var name = 'hi';
var is = 'halo';
[other code]
I want something to return the value of the var "my" in Python
How can I achieve that?
原文:https://stackoverflow.com/questions/41020606
2020-09-23 10:09
满意答案
最简单的方法是使用正则表达式模式通过BeautifulSoup定位元素并提取所需的子字符串:
import re
from bs4 import BeautifulSoup
data = """
[other code]
var my = 'hello';
var name = 'hi';
var is = 'halo';
[other code]
"""
soup = BeautifulSoup(data, "html.parser")
pattern = re.compile(r"var my = '(.*?)';$", re.MULTILINE | re.DOTALL)
script = soup.find("script", text=pattern)
print(pattern.search(script.text).group(1))
打印hello 。
The simplest approach is to use a regular expression pattern to both locate the element via BeautifulSoup and extract the desired substring:
import re
from bs4 import BeautifulSoup
data = """
[other code]
var my = 'hello';
var name = 'hi';
var is = 'halo';
[other code]
"""
soup = BeautifulSoup(data, "html.parser")
pattern = re.compile(r"var my = '(.*?)';$", re.MULTILINE | re.DOTALL)
script = soup.find("script", text=pattern)
print(pattern.search(script.text).group(1))
Prints hello.
2016-12-07
相关问答
您将空字符串传递给bs4,因为print(r.read())已将指针移动到文件末尾,删除print(r.read())并将其传递给BeautifulSoup(或调用r.seek(0)并传递。一旦你调用read,readlines或迭代文件对象,迭代器就会消耗掉,所以没有什么可以读的。 You are passing an empty string to bs4 because print(r.read()) has moved the pointer to the end of the file,...
将行index += 1缩小一级,将其移到while循环之外。 你也可以通过完全消除这个变量使循环更“Pythonic”,并用for替换: for p in page_source:
conference = p.div.h4.text
for team in p.find_all("a", {"class": "bi"}):
team_list.append(team.text)
team_link_list.append(team.get('h...
你需要使用BeautifulSoup 。 所以,假设你想提取value属性的value 。 以下是你如何做到的: import BeautifulSoup
import urllib2
request = "http://example.com"
source = urllib2.urlopen(request).read().decode()
# Or you can test with:
# source = "
这里的想法是找到具有Generic line list文本的元素。 然后,通过find_next_sibling()找到下一个ul兄弟,并通过find_next_sibling()获取所有链接: h3 = soup.find('h3', text='Generic Line List')
generic_line_list = h3.find_next_sibling('ul')
for link in generic_line_list.find_all('a', href=True):
...
使用contents BeautifulSoup Doc - .contents和.children for line in desc:
li_list = line.findAll('li')
for li in li_list:
print "%s%s" % (li.contents[0],li.b)
测试 from bs4 import BeautifulSoup
html ="""
Generally voted for equal gay ...您面临的问题是元素是由JS创建的,并且可能需要一些时间来加载它们。 你需要一个处理JS的刮板,并且可以等到所需的元素被创建。 你可以使用PyQt4 。 从webscraping.com和像BeautifulSoup这样的HTML解析器调整这个配方 ,这很简单: (写完之后,我找到了Python的webscraping库,可能值得一看) import sys
from bs4 import BeautifulSoup
from PyQt4.QtGui import *
from PyQt4.QtCo...
如果您不打算像@Ali建议的那样通过selenium尝试浏览器自动化,则必须解析包含所需信息的javascript 。 你可以用不同的方式做到这一点。 这是一个工作代码,它通过正则表达式模式定位script ,然后提取profile对象,将其与json一起加载到Python字典中并打印出所需的排名: import re
import json
from bs4 import BeautifulSoup
import requests
response = requests.get("http...
我不确定您希望如何区分其他链接中的视频,但您可以使用以下功能从文档中删除所有网址, def all_links(content):
links = []
url_re = re.compile(r'"(https?://[^"]*)"')
for m in url_re.finditer(content):
links.append(m.group(1))
return links
(这可以写成一行列表理解,但这种方式更具可读性。) 要根据JS对象...
首先,您需要以w模式打开文件。 而且,你需要写str(soup)或soup.prettify() : with open(r"C:\Users\rbaden\desktop\KPI_Site\index.html", "w") as f:
f.write(soup.prettify())
First, you need to open the file in w mode. And, you need to either write str(soup) or soup.prettify(...
最简单的方法是使用正则表达式模式通过BeautifulSoup定位元素并提取所需的子字符串: import re
from bs4 import BeautifulSoup
data = """
[other code]
var my = 'hello';
var name = 'hi';
var is = 'halo';
[other code]
"""
soup = BeautifulSoup(data, "html.parser")
patter...
相关文章
ByadminOnSeptember 10, 2012·Add Comment
...
原文地址:http://blog.chinaunix.net/uid-25525723-id-3630
...
java数组类型 ==> 基本数据类型 和 引用数据类型. js中 类型也分为两种 ==>
...
要让小球动起来,得了解js事件处理机制,通过键盘点击的事件来控制小球移动,在HTML5网页游戏坦克大战
...
报的是错误是 drawcharts is not defined ,但是我的定义没错啊? js代码
...
Hi Pythonistas! 测试和调试 Testing & Debuggi
...
lunr.js 实现了在网页上的搜索引擎,类似 Solr。 示例代码: view sourc
...
最近在研究微信的二次开发,因为要达到只能用微信的窗口打开页面,在网上找了很多资料都没有用JS写的,因为
...
js中2个等号与3个等号的区别 2011-06-24 14:14:09|分类:javascrip
...
python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速
...
最新问答
如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re
第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型
这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;
问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是
我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar
Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/
你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV
12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar
这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定
更多推荐


所有评论(0)