标签: beautifulsoup

在BeautifulSoup中匹配id

我正在使用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)

python beautifulsoup

23
推荐指数
2
解决办法
2万
查看次数

BeautifulSoup:如何从包含一些嵌套<ul>的<ul>列表中提取所有<li>?

我的源代码如下:

<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)

python screen-scraping beautifulsoup

23
推荐指数
2
解决办法
4万
查看次数

提取没有class属性的元素

我需要导航到特定类型的html元素.但是,页面上有许多这种类型的元素,有许多不同的类.

我需要一个没有任何类属性的.

我应该找一个class == '',还是有其他方式?

python beautifulsoup

23
推荐指数
2
解决办法
2万
查看次数

使用BeautifulSoup查找特定标签

我可以轻松地使用BS遍历通用标签,但我不知道如何查找特定标签.例如,我怎样才能找到所有出现的<div style="width=300px;">?这可能与BS有关吗?

python beautifulsoup

22
推荐指数
2
解决办法
4万
查看次数

使用BeautifulSoup获取第n个元素

从一张大桌子我想阅读第5,10,15,20行....使用BeautifulSoup.我该怎么做呢?findNextSibling和递增计数器的方法是什么?

python beautifulsoup web-scraping

22
推荐指数
2
解决办法
2万
查看次数

beautifulsoup findAll find_all

我想用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 beautifulsoup html-parsing xml-parsing

22
推荐指数
2
解决办法
2万
查看次数

当默认目录是python2.7时,如何将beautifulsoup安装到python3中?

我安装了python 2.7,python3.5.当我输入"pip install beautifulsoup4"它告诉我,它已经安装在python2.7/site-package目录中.

但是如何将其安装到python3目录中呢?

python pip beautifulsoup python-3.x

22
推荐指数
3
解决办法
5万
查看次数

BeautifulSoup可以以不区分大小写的方式工作吗?

我正在尝试为获取的网页提取元描述.但在这里,我面临的是BeautifulSoup区分大小写的问题.

有些页面有<meta name="Description,有些有<meta name="description.

我的问题非常类似于Stackoverflow上的问题

唯一的区别是我不能使用lxml ..我必须坚持使用Beautifulsoup.

python beautifulsoup

21
推荐指数
4
解决办法
4829
查看次数

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">

python beautifulsoup

21
推荐指数
3
解决办法
5万
查看次数

对于beautifulsoup,"单元测试失败了"

我正在尝试为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)

那么我能为这个错误做些什么,有人能建议我吗?

python beautifulsoup python-3.x

21
推荐指数
1
解决办法
1万
查看次数