我正在使用nextSibling遍历某个级别的元素来解析HTML中的一些数据,并根据遇到的每个元素的标记名称和类来执行不同的操作.
例如,
if n.name == "p" and n.class == "poem": blah()
Run Code Online (Sandbox Code Playgroud)
但是如果元素没有类或者它不是Tag的实例而因此没有名称,则会引发错误.
在访问之前进行测试
if "name" in n:
Run Code Online (Sandbox Code Playgroud)
总是返回假.我可以检查nextSibling返回的对象的类型,试图清除NavigableString和Comment,但必须有一个更简单的方法.
编辑
通过这个问题通过电子邮件发送了BeautifulSoup的开发者,他推荐使用
n.get("class")
Run Code Online (Sandbox Code Playgroud)
如果未设置"class",则返回None,这样就可以:
if n.get("class") == "poem": blah()
Run Code Online (Sandbox Code Playgroud) 我需要从HTML文件中提取数据.有问题的文件很可能是自动生成的.我已将其中一个文件的代码上传到Pastebin:http://pastebin.com/9Nj2Edfv.这是指向实际页面的链接:http://eur-lex.europa.eu/Notice.do?checktexts = checkbox&val = 60504%3A&call = 1& page = 1&lang = en&pgs = 10&nbl = 1&list = 60504%3Acs%C2&hwords =&action = GO&VISU =%23texte
我需要提取的数据可以在不同的标题下找到.
这是我到目前为止:
from BeautifulSoup import BeautifulSoup
ecj_data = open("data\ecj_1.html",'r').read()
soup = BeautifulSoup(ecj_data)
celex = soup.find('h1')
auth_lang = soup('ul', limit=14)[13].li
procedure = soup('ul', limit=20)[17].li
print "Celex number:", celex.renderContents(),
print "Authentic language:", auth_lang
print "Type of procedure:", procedure
Run Code Online (Sandbox Code Playgroud)
我将所有数据存储在本地,这就是它打开文件ecj_1.html的原因.
Celex数字和Authentic语言有点好用.
celex回归
"Celex number:
61977J0059"
Run Code Online (Sandbox Code Playgroud)
auth_lang返回 "Authentic language: <li>French</li>"
我只需要h1标签的内容(不是最后的中断).
[另外,我需要auth_lang只返回"法语",而不是<li>-tags.] …
commentary = soup.find('div', {'id' : 'live-text-commentary-wrapper'})
findtoure = commentary.find(text = re.compile('Gnegneri Toure Yaya')).replace('Gnegneri Toure Yaya', 'Yaya Toure')
Run Code Online (Sandbox Code Playgroud)
评论包含需要改为Yaya Toure的Gnegneri Toure Yaya的各种情况.
findAll() 不起作用,因为findtoure是一个列表.
另一个问题我是这样的代码只是发现他们并且替换它们进入一个新的变量,名为findtoure,我需要更换他们原有的汤.
我想我只是从错误的角度看待这个问题.
在这里问我,我正在尝试在这里为标签添加一个属性,想知道我是否可以使用BeautifulSoup方法,或者应该使用普通的字符串操作.
一个例子可能会说明这一点,因为这是一个奇怪的解释.
HTML代码现在的样子:
<option value="BC">BRITISH COLUMBIA</option>
Run Code Online (Sandbox Code Playgroud)
我希望它看起来如何:
<option selected="" value="BC">BRITISH COLUMBIA</option>
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!
我有:
<h2 id='names'>Names</h2>
<p>John</p>
<p>Peter</p>
Run Code Online (Sandbox Code Playgroud)
如果我已经拥有h2标签,那么现在最简单的方法就是将Peter带到这里?现在我试过了:
soup.select("#names > p:nth-child(1)")
Run Code Online (Sandbox Code Playgroud)
但在这里我得到了nth-child NotImplementedError:
NotImplementedError: Only the following pseudo-classes are implemented: nth-of-type.
Run Code Online (Sandbox Code Playgroud)
所以我不确定这里发生了什么.第二种选择是让所有'p'标记子项和硬选择[1]但是那时存在索引超出范围的危险,这将需要围绕每次尝试以获得彼得的尝试/除了有点愚蠢.
有没有办法用soup.select()函数选择nth-child?
编辑: 用nth-type替换nth-child似乎可以解决问题,所以正确的行是:
soup.select("#names > p:nth-of-type(1)")
Run Code Online (Sandbox Code Playgroud)
不确定为什么它不接受nth-child但似乎nth-child和nth-of-type返回相同的结果.
我有这个
from urllib import request
url = "http://www.bbc.co.uk/news/election-us-2016-35791008"
html = request.urlopen(url).read().decode('utf8')
html[:60]
from bs4 import BeautifulSoup
raw = BeautifulSoup(html, 'html.parser').get_text()
raw.find_all('title', limit=1)
print (raw.find_all("title"))
'<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN'
Run Code Online (Sandbox Code Playgroud)
我想使用 BeautifulSoup 提取页面的标题但收到此错误
Traceback (most recent call last):
File "C:\Users\Passanova\AppData\Local\Programs\Python\Python35-32\test.py", line 8, in <module>
raw.find_all('title', limit=1)
AttributeError: 'str' object has no attribute 'find_all'
Run Code Online (Sandbox Code Playgroud)
请任何建议
嗨所以我在a上应用find_all beautifulsoup object,并找到一些东西,这是一个bs4.element.ResultSet object或一个list.
我想在那里进一步做find_all,但是不允许这样做 bs4.element.ResultSet object.我可以循环遍历bs4.element.ResultSet objectfind_all的每个元素.但是我可以避免循环并将其转换回来beautifulsoup object吗?
请参阅代码了解详情.谢谢
html_1 = """
<table>
<thead>
<tr class="myClass">
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
</table>
"""
soup = BeautifulSoup(html_1, 'html.parser')
type(soup) #bs4.BeautifulSoup
# do find_all on beautifulsoup object
th_all = soup.find_all('th')
# the result is of type bs4.element.ResultSet or similarly list
type(th_all) #bs4.element.ResultSet
type(th_all[0:1]) #list
# now I want to further do find_all
th_all.find_all(text='A') #not work
# can I avoid this …Run Code Online (Sandbox Code Playgroud) 我怎样才能得到第一个孩子?
<div class="cities">
<div id="3232"> London </div>
<div id="131"> York </div>
</div>
Run Code Online (Sandbox Code Playgroud)
我怎么去伦敦?
for div in nsoup.find_all(class_='cities'):
print (div.children.contents)
Run Code Online (Sandbox Code Playgroud)
AttributeError:'listiterator'对象没有属性'contents'
我想从另一个url中获取数据,我正在使用urllib和Beautiful Soup,我的数据在table标签内(我已经使用Firefox控制台计算).但是当我尝试使用他的id获取表时结果是None,那么我想这个表必须通过一些js代码动态添加.
我已经尝试了所有解析器 'lxml','html5lib'但仍然无法获取该表数据.
我还尝试了一件事:
web = urllib.urlopen("my url")
html = web.read()
soup = BeautifulSoup(html, 'lxml')
js = soup.find("script")
ss = js.prettify()
print ss
Run Code Online (Sandbox Code Playgroud)
结果:
<script type="text/javascript">
myPage = 'ETFs';
sectionId = 'liQuotes'; //section tab
breadCrumbId = 'qQuotes'; //page
is_dartSite = "quotes";
is_dartZone = "news";
propVar = "ETFs";
</script>
Run Code Online (Sandbox Code Playgroud)
但现在我不知道如何获取这些js变量的数据.
现在我有两个选项要么获得表内容ot得到js变量,其中任何一个都可以完成我的任务但不幸的是我不知道如何获得这些,所以请告诉我如何解决任何一个问题.
谢谢
在BeautifulSoup中,如果我想找到其类为span3的所有div,我只会这样做:
result = soup.findAll("div",{"class":"span3"})
Run Code Online (Sandbox Code Playgroud)
但是,在我的情况下,我想找到所有以span3开头的div,因此,BeautifulSoup应该找到:
<div id="span3 span49">
<div id="span3 span39">
Run Code Online (Sandbox Code Playgroud)
等等...
我如何实现我想要的目标?我熟悉正则表达式; 但是我不知道如何将它们用于美丽的汤,也没有通过BeautifulSoup的文档找到任何帮助.
beautifulsoup ×10
python ×9
html ×2
web-scraping ×2
attributes ×1
html-parsing ×1
javascript ×1
python-3.x ×1
tags ×1
urllib2 ×1