2016-05-02 8 views
0

私はちょうどPythonを学んでいます。私はこの1つの事を理解するのに何時間も費やしました。基本的には、私は繰り返し構造を持つHTML文書を持っており、私は各繰り返しから特定の要素を取り出すことを試みています。私は最初の要素を引き出す方法を考え出しましたが、私の人生は他の要素を引き出すことができません。最初の1つは、それが別のクラスを持っているので簡単ですが、残りのクラスはそうではありません。私が狂ってしまう前に助けてください。Texts by Beautifulsoup

以下は、htmlの繰り返しセクションです。私はできた最初のヘッダーを抜き出したい。私はまた、 "概要"と "リスク要因"を取得したい。ここで

<h2 xmlns="" class="classsection4" id="idp201558400">50044 (1) - Ubuntu 
 
6.06 LTS/8.04 LTS/9.04/9.10/10.04 LTS/10.10 : linux, 
 
linux-ec2, linux-source-2.6.15 vulnerabilities (USN-1000-1)</h2> 
 
<h2 xmlns="" class="classh1 " style="vertical-align: middle;"><!--[if mso]><img src="cid:#" width="1" height="25" border="0" style="display: block; float: left;"> 
 
\t \t \t <![endif]]]-->Synopsis</h2> 
 
<span xmlns="" class="classtext" style="color: #263645; font-weight: normal;">The remote Ubuntu host is missing one or more security-related patches.</span><h2 xmlns="" class="classh1 " style="vertical-align: middle;"><!--[if mso]><img src="cid:#" width="1" height="25" border="0" style="display: block; float: left;"> 
 
\t \t \t <![endif]]]-->Description</h2> 
 
<span xmlns="" class="classtext" style="color: #263645; font-weight: normal;">This is some description text. 
 
(CVE-2010-NNN2).</span><h2 xmlns="" class="classh1 " style="vertical-align: middle;"><!--[if mso]><img src="cid:#" width="1" height="25" border="0" style="display: block; float: left;"> 
 
\t \t \t <![endif]]]-->Solution</h2> 
 
<span xmlns="" class="classtext" style="color: #263645; font-weight: normal;">Update the affected packages.</span><h2 xmlns="" class="classh1 " style="vertical-align: middle;"><!--[if mso]><img src="cid:#" width="1" height="25" border="0" style="display: block; float: left;"> 
 
\t \t \t <![endif]]]-->Risk Factor</h2> 
 
<span xmlns="" class="classtext" style="color: #263645; font-weight: normal;">Critical</span><h2 xmlns="" class="classh1 " style="vertical-align: middle;"><!--[if mso]><img src="cid:#" width="1" height="25" border="0" style="display: block; float: left;"> 
 
\t \t \t <![endif]]]-->CVSS Base Score</h2> 
 
<span xmlns="" class="classtext" style="color: #263645; font-weight: normal;">10.0 (CVSS2#AV:N/AC:L/Au:N/C:C/I:C/A:C)</span><h2 xmlns="" class="classh1 " style="vertical-align: middle;"><!--[if mso]><img src="cid:#" width="1" height="25" border="0" style="display: block; float: left;"> 
 
\t \t \t <![endif]]]-->CVSS Temporal Score</h2> 
 
<span xmlns="" class="classtext" style="color: #263645; font-weight: normal;">8.7 (CVSS2#E:ND/RL:OF/RC:ND)</span><h2 xmlns="" class="classh1 " style="vertical-align: middle;"><!--[if mso]><img src="cid:#" width="1" height="25" border="0" style="display: block; float: left;">

私の現在のコードです:

import requests 
from bs4 import BeautifulSoup 
import urllib 
import re 

page = open("C:/Users/AlphaWP/Downloads/631_SupportingFiles4_Labs6-7/Nessus Vulnerability Scan.htm").read() 

soup = BeautifulSoup(page, "html.parser") 

for section in soup.findAll("h2",{"class":"classsection4"}): 
    # nextNode = section 
    # print(nextNode.name) 
    # print(section) 
    print(section.contents) 
    print("##############################") 
    # print(section.contents) 
    for section1 in soup.findAll('h2', text=re.compile(r'Risk')): 
     print(section1) 
     riskFactor = section1.find("span") 
     riskLevel = riskFactor.contents 
     print(riskLevel) 
    print("##############################") 

答えて

0

要素が使用するすべてのスパンを取得するには:

spans = soup.find_all('span', {'class': 'classtext'}) 

spansは今クラスを持つすべてのスパン要素のリストであるclasstextSynopsisスパンとRisk Factorスパンにアクセスするには:

>>> spans[0] 
<span class="classtext" style="color: #263645; font-weight: normal;" xmlns="">The remote Ubuntu host is missing one or more security-related patches.</span> 
>>> spans[3] 
<span class="classtext" style="color: #263645; font-weight: normal;" xmlns="">Critical</span> 
+0

すごいああ、それはそのように働い実現しませんでした!ありがとうたくさんの男!!!! :) – bentsh

+0

@bentshよろしくお願いします。 :) – Pranav