我正在使用2.7.8并且有点惊讶bcz我得到所有文本但是包含在最后的文本<“br”>没有得到。就像我的html页面:
<html>
<body>
<div class="entry-content" >
<p>Here is a listing of C interview questions on “Variable Names” along with answers, explanations and/or solutions:
</p>
<p>Which of the following is not a valid C variable name?<br>
a) int number;<br>
b) float rate;<br>
c) int variable_count;<br>
d) int $main;</p> <!--not getting-->
<p> more </p>
<p>Which of the following is true for variable names in C?<br>
a) They can contain alphanumeric characters as well as special characters<br>
b) It is not an error to declare a variable to be one of the keywords(like goto, static)<br>
c) Variable names cannot start with a digit<br>
d) Variable can be of any length</p> <!--not getting -->!
</div>
</body>
</html>
和我的代码:
url = "http://www.sanfoundry.com/c-programming-questions-answers-variable-names-1/"
#url="http://www.sanfoundry.com/c-programming-questions-answers-variable-names-2/"
req = Request(url)
resp = urllib2.urlopen(req)
htmls = resp.read()
from bs4 import BeautifulSoup
soup = BeautifulSoup(htmls)
for br in soup.findAll('br'):
next = br.nextSibling
if not (next and isinstance(next,NavigableString)):
continue
next2 = next.nextSibling
if next2 and isinstance(next2,Tag) and next2.name == 'br':
text = str(next).strip()
if text:
print "Found:", next.encode('utf-8')
# print '...........sfsdsds.............',answ[0].encode('utf-8') #
输出:
Found:
a) int number;
Found:
b) float rate;
Found:
c) int variable_count;
Found:
a) They can contain alphanumeric characters as well as special characters
Found:
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
Found:
c) Variable names cannot start with a digit
但是我没有得到最后一个“文本”,例如:
d) int $main
and
d) Variable can be of any length
在&lt;“br”&gt;
之后我想要获得的输出:
Found:
a) int number;
Found:
b) float rate;
Found:
c) int variable_count;
Found:
d) int $main
Found:
a) They can contain alphanumeric characters as well as special characters
Found:
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
Found:
c) Variable names cannot start with a digit
d) Variable can be of any length
答案 0 :(得分:1)
这是因为BeautifulSoup通过在<br>
之前关闭</p>
标记将文本强制为有效的xml。美化版本很清楚:
<p>
Which of the following is not a valid C variable name?
<br>
a) int number;
<br>
b) float rate;
<br>
c) int variable_count;
<br>
d) int $main;
</br>
</br>
</br>
</br>
</p>
因此,d) int $main;
文字不是最后<br>
代码的兄弟,但是此代码的文字。
代码可以是(这里):
...
soup = BeautifulSoup(htmls)
for br in soup.findAll('br'):
if len(br.contents) > 0: # avoid errors if a tag is correctly closed as <br/>
print 'Found', br.contents[0]
它按预期给出:
Found
a) int number;
Found
b) float rate;
Found
c) int variable_count;
Found
d) int $main;
Found
a) They can contain alphanumeric characters as well as special characters
Found
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
Found
c) Variable names cannot start with a digit
Found
d) Variable can be of any length
答案 1 :(得分:1)
您可以使用Requests代替urllib2,并通过lxml的html模块提取xml。
from lxml import html
import requests
#request page
page=requests.get("http://www.sanfoundry.com/c-programming-questions-answers-variable-names-1/")
#get content in html format
page_content=html.fromstring(page.content)
#recover all text from <p> elements
items=page_content.xpath('//p/text()')
上面的代码返回<a>
元素中包含的文档中所有文本的数组
有了它,您可以简单地索引到数组中以打印您想要的内容。