我正在使用BeautifulSoup - python模块.我必须找到任何对id的引用,例如:'post-#'.例如:
<div id="post-45">...</div>
<div id="post-334">...</div>
Run Code Online (Sandbox Code Playgroud)
我该如何过滤?
html = '<div id="post-45">...</div> <div id="post-334">...</div>'
soupHandler = BeautifulSoup(html)
print soupHandler.findAll('div', id='post-*')
Run Code Online (Sandbox Code Playgroud) 我的源代码如下:
<h3>Header3 (Start here)</h3>
<ul>
<li>List items</li>
<li>Etc...</li>
</ul>
<h3>Header 3</h3>
<ul>
<li>List items</li>
<ul>
<li>Nested list items</li>
<li>Nested list items</li></ul>
<li>List items</li>
</ul>
<h2>Header 2 (end here)</h2>
Run Code Online (Sandbox Code Playgroud)
我希望所有"li"标签跟在第一个"h3"标签之后,并停在下一个"h2"标签,包括所有嵌套的li标签.
firstH3 = soup.find('h3')
正确找到我想要开始的地方.
firstH3 = soup.find('h3') # Start here
uls = []
for nextSibling in firstH3.findNextSiblings():
if nextSibling.name == 'h2':
break
if nextSibling.name == 'ul':
uls.append(nextSibling)
Run Code Online (Sandbox Code Playgroud)
给我一个UL列表,每个都有我需要的LI内容.
摘录"uls"列表:
<ul>
...
<li><i><a href="/wiki/Agent_Cody_Banks" title="Agent Cody Banks">Agent Cody Banks</a></i> (2003)</li>
<li><i><a href="/wiki/Agent_Cody_Banks_2:_Destination_London" title="Agent Cody Banks 2: Destination London">Agent Cody Banks 2: Destination …Run Code Online (Sandbox Code Playgroud) 我需要导航到特定类型的html元素.但是,页面上有许多这种类型的元素,有许多不同的类.
我需要一个没有任何类属性的.
我应该找一个class == '',还是有其他方式?
我可以轻松地使用BS遍历通用标签,但我不知道如何查找特定标签.例如,我怎样才能找到所有出现的<div style="width=300px;">?这可能与BS有关吗?
从一张大桌子我想阅读第5,10,15,20行....使用BeautifulSoup.我该怎么做呢?findNextSibling和递增计数器的方法是什么?
我想用Python解析一个html文件,我使用的模块是beautifulsoup.
在我使用它之后,发生了一些奇怪的事情.据说函数"find_all"是
和"findAll"一样,但我已经尝试过了.但它是不同的.
有谁能告诉我不同的?
import urllib, urllib2, cookielib
from BeautifulSoup import *
site = "http://share.dmhy.org/topics/list?keyword=TARI+TARI+team_id%3A407"
rqstr = urllib2.Request(site)
rq = urllib2.urlopen(rqstr)
fchData = rq.read()
soup = BeautifulSoup(fchData)
t = soup.findAll('tr')
Run Code Online (Sandbox Code Playgroud) 我安装了python 2.7,python3.5.当我输入"pip install beautifulsoup4"它告诉我,它已经安装在python2.7/site-package目录中.
但是如何将其安装到python3目录中呢?
我正在尝试为获取的网页提取元描述.但在这里,我面临的是BeautifulSoup区分大小写的问题.
有些页面有<meta name="Description,有些有<meta name="description.
我的问题非常类似于Stackoverflow上的问题
唯一的区别是我不能使用lxml ..我必须坚持使用Beautifulsoup.
我想提取内容"Hello world".请注意,页面上也有倍数Hello world和类似值<table>.
我尝试了以下方法:
<table border="0" cellspacing="2" width="800">
<tr>
<td colspan="2"><b>Name: </b>Hello world</td>
</tr>
<tr>
...
Run Code Online (Sandbox Code Playgroud)
但它没有任何回报.
这是代码的片段:
hello = soup.find(text='Name: ')
hello.findPreviousSiblings
Run Code Online (Sandbox Code Playgroud)
另外,我也有以下提取"我的家庭地址"的问题:
<td><b>Address:</b></td>
<td>My home address</td>
Run Code Online (Sandbox Code Playgroud)
我也使用相同的方法来搜索text ="Address:"但是我如何导航到下一行并提取内容<td colspan="2">?
我正在尝试为python33安装beautifulsoup但它没有正确安装它给出了这样的错误:
C:\Python33>pip install beautifulsoup
Downloading/unpacking beautifulsoup
Downloading BeautifulSoup-3.2.1.tar.gz
Running setup.py egg_info for package beautifulsoup
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "c:\windows\temp\pip_build_Prashant\beautifulsoup\setup.py", line 22
print "Unit tests have failed!"
^
SyntaxError: invalid syntax
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "c:\windows\temp\pip_build_Prashant\beautifulsoup\setup.py", line 22
print "Unit tests have failed!"
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
那么我能为这个错误做些什么,有人能建议我吗?
beautifulsoup ×10
python ×10
python-3.x ×2
html-parsing ×1
pip ×1
web-scraping ×1
xml-parsing ×1