Python 使用BeautifulSoup获取下一个UL元素
我试图在给定网页中找到下一个ul元素 我首先将我的反应插入到美丽的汤中,如下所示:Python 使用BeautifulSoup获取下一个UL元素,python,beautifulsoup,Python,Beautifulsoup,我试图在给定网页中找到下一个ul元素 我首先将我的反应插入到美丽的汤中,如下所示: soup = BeautifulSoup(response.context) 打印出response.context将提供以下内容 print(response.context) <!DOCTYPE html> <html> <head> <title> | FollowUp</title> <meta n
soup = BeautifulSoup(response.context)
打印出response.context将提供以下内容
print(response.context)
<!DOCTYPE html>
<html>
<head>
<title> | FollowUp</title>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link href='/static/css/bootstrap.min.css' rel='stylesheet' media='screen'>
</head>
<body>
<div class='navbar'>
<div class='navbar-inner'>
<a class='brand' href='/'>TellMe.cat</a>
<ul class='nav'>
<li><a href='list'>My Stories</a></li>
<li><a href='add'>Add Story</a></li>
<li><a href='respond'>Add Update</a></li>
</ul>
<form class='navbar-form pull-right' action='process_logout' method='post'>
<input type='hidden' name='csrfmiddlewaretoken' value='RxquwEsaS5Bn1MsKOIJP8uLtRZ9yDusH' />
Hello add!
<button class='btn btn-small'>Logout</button>
</form>
</div>
</div>
<div class='container'>
<ul id='items'>
<ul>
<li><a href='http://www.example.org'>http://www.example.org</a></li>
<ul>
<p>There have been no follow ups.</p>
</ul>
</ul>
</ul>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src='/static/js/bootstrap.min.js'></script>
</body>
</html>
这给了我正确的ul和它所有的孩子。无论如何
items.find_next('ul')
给出了
TypeError: 'NoneType' object is not callable
尽管这似乎是它被称为美丽的汤医生的方式:
我做错了什么?创建一个,pip安装BeautifulSoup请求,打开python控制台
import BeautifulSoup
import requests
html = requests.get("http://yahoo.com").text
b = BeautifulSoup.BeautifulSoup(html)
m = b.find(id='masthead')
item = m.findNext('ul')
dir(m)
告诉您m
上的函数。您可以看到您想要findNext
您还可以找到一个更宽容的shell来运行python。您可以键入变量的名称,然后点击Tab键查看成员变量。您不能执行find\u all
,返回列表吗?我不能,因为它会给出相同的错误。项目属于BeautifulSoup.Tag类型,而不是非类型dir()
是您的朋友。或者在ipython中运行此命令并使用tab completion。
import BeautifulSoup
import requests
html = requests.get("http://yahoo.com").text
b = BeautifulSoup.BeautifulSoup(html)
m = b.find(id='masthead')
item = m.findNext('ul')