我想得到所有<a>孩子的标签<li>
<div>
<li class="test">
<a>link1</a>
<ul>
<li>
<a>link2</a>
</li>
</ul>
</li>
</div>
Run Code Online (Sandbox Code Playgroud)
我知道如何找到像这样的特定类的元素
soup.find("li", { "class" : "test" })
Run Code Online (Sandbox Code Playgroud)
但我不知道如何找到所有<a>孩子<li class=test>而不是其他孩子
喜欢我想选择
<a>link1</a>
Run Code Online (Sandbox Code Playgroud) 我正在使用BeautifulSoup来抓取一个网址,我有以下代码
import urllib
import urllib2
from BeautifulSoup import BeautifulSoup
url = "http://www.example.com/servlet/av/ResultTemplate=AVResult.html"
req = urllib2.Request(url)
response = urllib2.urlopen(req)
the_page = response.read()
soup = BeautifulSoup(the_page)
soup.findAll('td',attrs={'class':'empformbody'})
Run Code Online (Sandbox Code Playgroud)
现在在上面的代码中我们可以findAll用来获取与它们相关的标签和信息,但我想使用xpath.是否可以将xpath与BeautifulSoup一起使用?如果可能的话,有人可以给我一个示例代码,以便更有帮助吗?
我试图在网页上的特定"输入"标签中提取单个"值"属性的内容.我使用以下代码:
import urllib
f = urllib.urlopen("http://58.68.130.147")
s = f.read()
f.close()
from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(s)
inputTag = soup.findAll(attrs={"name" : "stainfo"})
output = inputTag['value']
print str(output)
Run Code Online (Sandbox Code Playgroud)
我得到一个TypeError:列表索引必须是整数,而不是str
即使从Beautifulsoup文档我明白字符串不应该是一个问题...但我没有专家,我可能会误解.
任何建议都非常感谢!提前致谢.
可以使用BeautifulSoup从HTML中删除脚本标记及其所有内容,还是必须使用正则表达式或其他内容?
我想根据其名称打印属性值,例如
<META NAME="City" content="Austin">
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情
soup = BeautifulSoup(f) //f is some HTML containing the above meta tag
for meta_tag in soup('meta'):
if meta_tag['name'] == 'City':
print meta_tag['content']
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了一个KeyError: 'name',我相信这是因为BeatifulSoup使用了name,所以它不能用作关键字参数.
我已经使用easy_install安装了BeautifulSoup并尝试运行以下脚本
from BeautifulSoup import BeautifulSoup
import re
doc = ['<html><head><title>Page title</title></head>',
'<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
'<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
'</html>']
soup = BeautifulSoup(''.join(doc))
print soup.prettify()
Run Code Online (Sandbox Code Playgroud)
但不确定为什么会这样
Traceback (most recent call last):
File "C:\Python27\reading and writing xml file from web1.py", line 49, in <module>
from BeautifulSoup import BeautifulSoup
ImportError: No module named BeautifulSoup
Run Code Online (Sandbox Code Playgroud)
能否请你帮忙.谢谢
我如何使用BeautifulSoup搜索仅包含我搜索的属性的标签?
例如,我想找到所有<td valign="top">标签.
以下代码:
raw_card_data = soup.fetch('td', {'valign':re.compile('top')})
获取我想要的所有数据,但也获取<td>具有该属性的任何标记valign:top
我也试过了:
raw_card_data = soup.findAll(re.compile('<td valign="top">'))
这没有任何回报(可能是因为正则表达式不好)
我想知道在BeautifulSoup中是否有一种方法可以说"查找<td>唯一属性为valign:top"的标签
更新
例如,如果HTML文档包含以下<td>标记:
<td valign="top">.....</td><br />
<td width="580" valign="top">.......</td><br />
<td>.....</td><br />
Run Code Online (Sandbox Code Playgroud)
我只想要第一个<td>tag(<td width="580" valign="top">)返回
我正在学习python requests和BeautifulSoup.对于练习,我选择写一个快速的纽约市停车票解析器.我能得到一个非常难看的HTML回复.我需要抓住lineItemsTable并解析所有的门票.
你可以通过这里重现页面:https://paydirect.link2gov.com/NYCParking-Plate/ItemSearch并进入一个NY盘子T630134C
soup = BeautifulSoup(plateRequest.text)
#print(soup.prettify())
#print soup.find_all('tr')
table = soup.find("table", { "class" : "lineItemsTable" })
for row in table.findAll("tr"):
cells = row.findAll("td")
print cells
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?简单的寻找所有tr不会让我在任何地方.
我正在尝试使用pipPython 2.7 安装BeautifulSoup .我一直收到错误信息,无法理解原因.
我按照说明安装了pip,它安装在以下目录中:c:\Python27\Scripts\pip.exe然后我尝试将它添加到路径中,然后运行pip install package命令.
尝试了两种不同的方式:
import sys
sys.path.append('C:\\Python27\\Scripts\\pip.exe')
pip install beautifulsoup4
import sys
sys.path.append('C:\\Python27\\Scripts')
pip install beautifulsoup4
Run Code Online (Sandbox Code Playgroud)
两个都给我这个错误信息:
>>> pip install beautifulsoup4
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
shell突出显示"安装"一词,并说它是无效的语法.
我不知道发生了什么,所以任何帮助都将不胜感激.
beautifulsoup ×10
python ×10
html ×2
attributes ×1
parsing ×1
pip ×1
python-2.7 ×1
text ×1
urllib ×1
xpath ×1