python
  • html
  • beautifulsoup
  • 2016-10-11 73 views 2 likes 
    2

    這是運行soup[0].find_all('div', {'class':'font-160 line-110'})ResultSet爲什麼BeautifulSoup工作第二次解析,而不是第

    [<div class="font-160 line-110" data-container=".snippet-container" data-html="true" data-placement="top" data-template='&lt;div class="tooltip infowin-tooltip" role="tooltip"&gt;&lt;div class="tooltip-arrow"&gt;&lt;div class="tooltip-arrow-inner"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="tooltip-inner" style="text-align: left"&gt;&lt;/div&gt;&lt;/div&gt;' data-toggle="tooltip" title="XIAMEN [CN]">  
        <a class="no-underline group-ib color-inherit" 
        href="/en/ais/details/ports/959"> 
        <span class="text-default">CN</span><span class="text-default text-darker">XMN 
    </span> 
    </a> 
    </div>] 
    

    在試圖拔出XIAMEN [CN]title後,我無法使用a[0].find('div')['title](其中a是在BeautifulSoup ResultSet以上)。但是,如果我複製並粘貼HTML作爲一個新的字符串,比方說,

    b = '''<div class="font-160 line-110" data-container=".snippet container" data-html="true" data-placement="top" data-template='&lt;div class="tooltip infowin-tooltip" role="tooltip"&gt;&lt;div class="tooltip-arrow"&gt;&lt;div class="tooltip-arrow-inner"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="tooltip-inner" style="text-align: left"&gt;&lt;/div&gt;&lt;/div&gt;' data-toggle="tooltip" title="XIAMEN [CN]">''' 
    

    然後做:

    >>soup = BeautifulSoup(b, 'html.parser') 
    >>soup.find('div')['title'] 
    >>XIAMEN [CN] #prints contents of title 
    

    爲什麼我要reSoup的湯?爲什麼這不適用於我的第一次搜索?

    編輯的soup產地:

    我有urls列表,我通過grequests要去雖然。我正在尋找的東西之一是title,其中包含XIAMEN [CN]

    所以soup的網址是

    [ 
    'http://www.marinetraffic.com/en/ais/details/ships/shipid:564352/imo:9643752/mmsi:511228000/vessel:DE%20MI', 
    'http://www.marinetraffic.com/en/ais/details/ships/shipid:3780155/imo:9712395/mmsi:477588800/vessel:SITC%20GUANGXI?cb=2267' 
    ] 
    
    +0

    你在用湯[0]做什麼?它是什麼?很確定你正在做兩件完全不同的事情。 –

    +0

    「湯」是ResultSet的列表,我使用'soup [0]'來訪問該列表的第一個條目。 –

    +0

    你可以添加HTML或鏈接到頁面,因爲我非常自信你做錯了什麼。 –

    回答

    1

    我發現了當我設置我的BeautifulSoup時發生的問題。我創建了部分搜索結果列表,然後必須迭代列表來研究它。我解決了這個問題的只是在尋找什麼,我想在行:

    我改變:

    soup = [] 
        with i in range(2) #number of pages parsed 
        rawSoup = BeautifulSoup(response[i].content, 'html.parser') 
        souporigin = rawSoup.find_all('div', {'class': 'bg-default bg-white no- snippet-hide'}) 
        soup.append(souporigin) 
    

    到:

    a = soup.find("div", class_='font-160 line-110')["title"] 
    

    而一旦創建我soup這消除運行此搜索代碼中有很多冗餘 - 我一直在創建ResultSets的列表,並且不得不在他們的新字段上使用find

    0

    你用錯誤的選擇創建時我做了

    soup = [] 
        with i in range(2) #number of pages parsed 
        rawSoup = BeautifulSoup(response[i].content, 'html.parser') 
        souporigin = rawSoup.find_all('div', {'class': 'bg-default bg-white no- snippet-hide'}) 
        soup.append(souporigin) 
    

    選擇soup[0].find_all('div', {'class':'font-160 line-110'})找到<div>,您甚至可以在打印時看到<div>。但是當你添加.find()它開始搜索<div> - 所以.find('div')嘗試在當前div

    尋找新div你需要

    a[0]['title'] 
    

    當你創建新的湯,然後主/根元素是不是div,而是[document]div是它的孩子(div在主「標籤」內),因此您可以使用find('div')

    相關問題