簡體   English   中英

如何在兩者之間提取文本 <br> 蟒蛇

[英]How to extract text in between <br> python

我從鏈接http://www.sanfoundry.com/c-programming-questions-answers-variable-names-1/中通過“檢查元素”提取了一些div。 div<p>和在<P>有一些文本,其通過<BR>和我試圖提取那些文本,這樣我可以在通過one.I陣列或分貝一個地方是卡住是斷開線在提取需要文本其之前和之后的<BR>。

<div class="entry-content" style="visibility: visible; opacity: 1;">
 <div style="text-align:justify">
This section on C interview <span id="IL_AD1" class="IL_AD">questions and answers</span> focuses on “Variable Names”. One shall practice these <span id="IL_AD5" class="IL_AD">interview questions</span> to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C Interview questions come with detailed explanation of the <span id="IL_AD2" class="IL_AD">answers</span> which helps in better understanding of C <span id="IL_AD3" class="IL_AD">concepts</span>.<p></p>
<p>Here is a listing of C interview questions on “Variable Names” along with answers, explanations and/or solutions:
</p></div>
<p>1. C99 standard guarantees uniqueness of ____ characters for internal names.<br>
a) 31<br>
b) 63<br>
c) 12<br>
d) 14</p>
<span class="collapseomatic" id="id5489" tabindex="0" title="View Answer">View Answer</span><div id="target-id5489" class="collapseomatic_content " style="display: none;">Answer:b<br>
Explanation:ISO C99 compiler may consider only first 63 characters for internal.<br>
</div>
<p>2. C99 standard guarantess uniqueness of _____ characters for external names.<br>
a) 31<br>
b) 6<br>
c) 12<br>
d) 14</p>
<span class="collapseomatic " id="id7970" tabindex="0" title="View Answer">View Answer</span><div id="target-id7970" class="collapseomatic_content " style="display: none;">Answer:a<br>
Explanation:ISO C99 compiler may consider only first 31 characters for external<br>
variables having 31 characters due to which it may not be unique.<br>
</div>
<p>3. Which of the following is not a valid variable name declaration?<br>
a) int __a3;<br>
b) int __3a;<br>
c) int __A3;<br>
d) None of the mentioned</p>
<span class="collapseomatic " id="id5714" tabindex="0" title="View Answer">View Answer</span><div id="target-id5714" class="collapseomatic_content " style="display: none;">Answer:d<br>
Explanation:None.<br>
</div>
<p>4. Which of the following is not a valid variable name declaration?<br>
a) int _a3;<br>
b) int a_3;<br>
c) int 3_a;<br>
d) int _3a</p>

因此,shell如何分別獲得“ C99標准保證內部____個字符的唯一性”“ 31”,“ 63”,“ 12”,“ 14”,“ C99標准保證外部____個字符的唯一性”和“ 31”, “ 6”,“ 12”,“ 14”等...

與此同時,我也不需要項目符號編號和字母順序

碼:

from bs4 import BeautifulSoup
soup = BeautifulSoup(htmls, 'html.parser')
h4s = soup.find_all('p')
for h4 in h4s:
    for text in h4.find_next_siblings('br'):
        print(text.strip())

知道我錯了嗎?

您可以在這種情況下嘗試CSS Selector soup.select('div.entry-content p')將選擇一個具有class(請參閱. )名稱entry-content div以及該div中的所有p 我假設只有一個div具有該類名稱。

from bs4 import BeautifulSoup as bs

html = """<div class="entry-content" style="visibility: visible; opacity: 1;">
 <div style="text-align:justify">
This section on C interview <span id="IL_AD1" class="IL_AD">questions and answers</span> focuses on “Variable Names”. One shall practice these <span id="IL_AD5" class="IL_AD">interview questions</span> to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C Interview questions come with detailed explanation of the <span id="IL_AD2" class="IL_AD">answers</span> which helps in better understanding of C <span id="IL_AD3" class="IL_AD">concepts</span>.<p></p>
<p>Here is a listing of C interview questions on “Variable Names” along with answers, explanations and/or solutions:
</p></div>
<p>1. C99 standard guarantees uniqueness of ____ characters for internal names.<br>
a) 31<br>
b) 63<br>
c) 12<br>
d) 14</p>
<span class="collapseomatic" id="id5489" tabindex="0" title="View Answer">View Answer</span><div id="target-id5489" class="collapseomatic_content " style="display: none;">Answer:b<br>
Explanation:ISO C99 compiler may consider only first 63 characters for internal.<br>
</div>
<p>2. C99 standard guarantess uniqueness of _____ characters for external names.<br>
a) 31<br>
b) 6<br>
c) 12<br>
d) 14</p>
<span class="collapseomatic " id="id7970" tabindex="0" title="View Answer">View Answer</span><div id="target-id7970" class="collapseomatic_content " style="display: none;">Answer:a<br>
Explanation:ISO C99 compiler may consider only first 31 characters for external<br>
variables having 31 characters due to which it may not be unique.<br>
</div>
<p>3. Which of the following is not a valid variable name declaration?<br>
a) int __a3;<br>
b) int __3a;<br>
c) int __A3;<br>
d) None of the mentioned</p>
<span class="collapseomatic " id="id5714" tabindex="0" title="View Answer">View Answer</span><div id="target-id5714" class="collapseomatic_content " style="display: none;">Answer:d<br>
Explanation:None.<br>
</div>
<p>4. Which of the following is not a valid variable name declaration?<br>
a) int _a3;<br>
b) int a_3;<br>
c) int 3_a;<br>
d) int _3a</p>"""

soup = bs(html,'html.parser')

p = soup.select('div.entry-content p')
for i in p[2:]:
    print i.text.encode('utf-8')
    print '\n'*3 # just print three newlines

輸出 -

1. C99 standard guarantees uniqueness of ____ characters for internal names.
a) 31
b) 63
c) 12
d) 14




2. C99 standard guarantess uniqueness of _____ characters for external names.
a) 31
b) 6
c) 12
d) 14




3. Which of the following is not a valid variable name declaration?
a) int __a3;
b) int __3a;
c) int __A3;
d) None of the mentioned




4. Which of the following is not a valid variable name declaration?
a) int _a3;
b) int a_3;
c) int 3_a;
d) int _3a

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM