草庐IT

beautifulSoup

全部标签

python - 为什么 BeautifulSoup 会修改我的自关闭元素?

这是我的脚本:importBeautifulSoupif__name__=="__main__":data=""""""soup=BeautifulSoup.BeautifulStoneSoup(data)printsoup运行时,打印:我希望它保持相同的结构。我该怎么做? 最佳答案 来自BeautifulSoupdocumentation:ThemostcommonshortcomingofBeautifulStoneSoupisthatitdoesn'tknowaboutself-closingtags.HTMLhasafixe

python - 使用 BeautifulSoup 提取相似的 XML 属性

假设我有以下XML:而我想从中收集timefrom、symbolname和temperaturevalue,然后按如下方式打印出来:timefrom:symbolname,tempraurevalue--像这样:2017-07-29,08:00:00:Cloudy,15°。(如您所见,此XML中有一些name和value属性。)到目前为止,我的方法非常简单:#!/usr/bin/envpython#coding:utf-8importrefromBeautifulSoupimportBeautifulSoup#dataissettotheaboveXMLsoup=BeautifulSo

python - 如何使用 beautifulsoup 获取原始文本?

我有这样一个xml:www.link1.comwww.link2.com我试过这段代码:fromBeautifulSoupimportBeautifulStoneSoupsoup=BeautifulStoneSoup(results2)#BeautifulSouplinklist=soup.findAll('link')printsoup使用这段代码,输出是[www.link1.com,www.link2.com]但我想要这样的输出[www.link1.com,www.link2.com] 最佳答案 你试过吗:linklist=[e

python - BeautifulSoup 迭代多个 XML 标签,提取字符串列表

#SampleXMLfile.xml="""SomecontentSomeothercontentSomemorecontentsSomecontentSomeothercontentSomemorecontentsSomecontentSomeothercontentSomemorecontents"""这是示例XML文件;我想处理所有标签。首先我需要找到所有1个标签,其次,以列表的形式获取内容。我希望是单独的列表元素。例如我期待像['','somecontent',''.....]这样的列表而不是这样['Somecontent',....]_frombs4importBeautif

python - 如何在 ATOM XML 文档中搜索大小写混合的标记名称?

我正在使用GoogleAPI,他们提供了返回JSON或ATOM的选项。ATOM看起来像XML语法,我想用BeautifulSoup来解析它。我可以毫无问题地将其转换为BeautifulSoup对象,但我很难找到该元素。以ATOM文档的一段话为例:frombs4importBeautifulSoupfeed=""""""soup=BeautifulSoup(feed)printsoup.find_all("cse:Attribute",{"value":"160"})...它返回一个空列表。我做错了什么? 最佳答案 您编写的代码将XM

python - 在 Python 中处理 `
`

问题背景:我有一个XML文件,我正在将其导入BeautifulSoup并进行解析。一个节点有以下内容:请注意,该值在文本中包含 和。我知道这些是回车和换行的XML表示。当我导入到BeautifulSoup时,值会转换为以下内容:您会注意到 被转换为换行符。我的用例要求该值保持原始值。知道如何让它留下来吗?或者将其转换回来?源代码:python:(2.7.11)frombs4importBeautifulSoup#version4.4.0s=BeautifulSoup(open('test.xml'),'lxml-xml',from_encoding="ansi")prints.DIAt

python - 如何让 Beautifulsoup 不添加 <html> 或 <?xml ?>

有没有办法让beautifulsoup不添加在xml文件的开头或标签?我读过bs4doc并尝试了xml、html和lxml解析器,但结果相似。我还测试了soup.find('?xml'),这不会返回任何内容。$pythonPython2.7.5(default,Aug22016,04:20:16)[GCC4.8.520150623(RedHat4.8.5-4)]onlinux2Type"help","copyright","credits"or"license"formoreinformation.>>>frombs4importBeautifulSoup>>>xml='value'>

python - 我如何阻止漂亮的汤在解析时跳过行?

在使用beautifulsoup解析html中的表格时,每隔一行以而不是没有类的tr标签示例HTMLItemA14.8k-555ItemB64.9k+165ItemC4,000+666我要提取的文本是14.8k、64.9k和4,000this1=urllib2.urlopen('myurl').read()this_1=BeautifulSoup(this1)this_1a=StringIO.StringIO()forrowinthis_1.findAll("tr",{"class":"row_k"}):forcolinrow.findAll(re.compile('td')):thi

python - 如何转义实际上名为 <parent> 的 BeautifulSoup ISO 标签中的父属性?

好吧,这有点有趣。这是XML:com.parentparent1.0-SNAPSHOT../pom.xmlsrc我想使用简单的BeautifulSoup到达实际名为的节点的分层表示法但是parent实际上是这个API中的一个保留属性标签。withopen(pom)aspomHandle:soup=BeautifulSoup(pomHandle)#thisreturnstheproperbuildnodebuildNode=soup.project.build#thisdoesnotreturntheproperparentnodebuttheXMLparentoftheprojectn

Python xml 遍历问题和答案

我将调查回复存储在xml中,不幸的是xml不是统一构建的。请参阅下面的xml。我想遍历div,然后拉出所有元素作为问题,但我不确定如何处理答案,因为它们有时包含在子中。有时不是。本来想用elementtree的intertext或者beautifulsoup。但是,如果我执行soup.find_all('div'),BeautifulSoup会返回所有div,包括内部的div。.tree.itertext()有点工作,但如果可能的话,我不想有太多的嵌套循环。有什么建议可以最好地处理这种情况吗?Question1:Whatisyourname?MynameisPeter.Question