嘎。我花了相当长的时间试图找到如何正确地,甚至粗鲁地做这件事,我只是被难住了。我从一个站点下载了2500多个HTML文件,我只需要从任何给定的页面提取有限的信息:页面描述的演讲标题(这样我就可以用我们已经拥有的一个巨大的CSV来整理这些数据),然后是给定演讲的事件,以及演讲发表的日期。在
这些页面的HTML是扩展的,并且填充了<script>
元素。我只想要后面跟q
的那个。开始此块的行如下所示:
<script>q("talkPage.init", {
下面是相当多的数据。我只需要这三样东西:
^{pr2}$幸运的是,"filmed"
和{
我的想法是使用BeautifulSoup来找到<script>q
元素,然后将其传递到json模块进行解析,但是我无法找到hot-tellsoup来获取<script>
元素,然后再加上一个q——类和id很容易。然后。。。没那么多。在
为了开始处理JSON部分,我创建了一个文本文件,其中只包含<script>q
元素的内容,但是我承认让JSON模块来加载它并不是很好。在
我为实验准备的代码首先用我感兴趣的JSON块加载文本文件,然后尝试对其进行解码,以便我可以使用它执行其他操作:
import json
text = open('dawkins_script_element.txt', 'r').read()
data = json.loads(text)
但是很明显JSON解码器不喜欢我所拥有的,因为它抛出了一个ValueError: Expecting value: line 1 column 1 (char 0)
。呸!在
此脚本元素的前三行如下所示:
<script>q("talkPage.init", {
"el": "[data-talk-page]",
"__INITIAL_DATA__":
这就是我目前的处境。如果能给汤或json提供帮助,我们将不胜感激。在
这是我最后使用的脚本,感谢@Idlehands和@Three。为了深入研究奇怪的单引号JSON,我将整个JSON元素读入一个列表中,并用逗号分隔。这是一个黑客,但基本上是有效的。在
在不了解全部情况的情况下,有一个穷人的尝试:
假设您的html如下所示:
您可以这样编写代码:
^{pr2}$然后您可以开始解释JSON:
这里有很多假设。假设
<script>q
元素中的所有文本始终以q("talkPage.init",
开头,以)
结尾。它还假设返回的文本遵循json格式,以便下一阶段的解析。我还假设您了解如何解析json结果。在可以使用正则表达式匹配所需的部分。在
要找到您需要的东西,您可以使用pythons json库来解析它,或者将最后的东西与您想要的匹配。因为}是唯一的,
^{pr2}$filmed
和{event
没有区别(据我所知?)在或
后者需要过滤一点,以除去尾随的},或者您不希望从中得到的内容。在
]"
和前面的{我听说beauthoulsoup也是一个很好的匹配html的库,但是我不太熟悉它。在
相关问题 更多 >
编程相关推荐