2016-12-30 7 views
1

私はテキストを取得するにはどうすればよい:「このテキストは、」ここでPythonのセレン - リンクのテキストを取得(A)

は、私が試したものです:

elem = browser.find_element_by_css_selector('td.mainContent > a') 
print(elem.text) 

は、これは以来、本当に混乱して私を残し私はこれがaのテキストを見つけるだろうと確信していましたが、そうではありませんでした。 ( "THIS TEXT" の検索HTMLでそれを見つけること。)

<tbody> 


<tr class="dbaListing listing hasInsertionFee"> 

    <td class="pictureColumn" colspan="1"> 
     <div class="thumbnailContainer"> 
      <a class="thumbnailContainerInner" href="http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/"> 

      <img class="thumbnail lazy image-placeholder" alt="Tastatur, Mad Catz, Strike 7, Perfekt, Den er helt ny, stadig i fuld indpakning. 

Hvorfor sælger jeg? 
Jeg opdagede at jeg havde skaffet det forkerte tastetur, der var ment som gave. Og siden at jeg havde ventet for længe med at få den byttet, var det altså for sent. Derfor sælger jeg den så for 1900 kr. (300 mindre en nypris) 

Den er virkelig alle pengene værd, og har så mange funktioner, at jeg aldrig kan komme tæt på at beskrive alt her. Derfor hvis i er interesserede i den kan i..." data-original="http://dbastatic.dk/pictures/pictures/3d/05/e060-37f5-4465-af3e-d20e730a9853.jpg?preset=srpgallery" src="http://dbastatic.dk/pictures/pictures/3d/05/e060-37f5-4465-af3e-d20e730a9853.jpg?preset=srpgallery" style="display: block;"> 
      </a> 
     </div> 
    </td> 

<td class="mainContent" colspan="2"> 
    <script type="application/ld+json"> 
    { 
     "@context": "http://schema.org", 
     "@type": "Product", 
     "name": "Tastatur, Mad Catz, Strike 7, Perfekt, Den er...", 
     "image": "http://dbastatic.dk/pictures/pictures/3d/05/e060-37f5-4465-af3e-d20e730a9853.jpg?preset=medium", 
     "url": "http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/", 
     "offers": { 
      "@type": "Offer", 
      "priceCurrency": "DKK", 
      "price": "1900" 
     } 
    } 
    </script> 

    <a class="listingLink" href="http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/"> 
      <span class="headline-block"> 
          </span> 

    </a> 

    <div class="expandable-box expandable-box-collapsed"> 
     <a class="listingLink" href="http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/">THIS TEXT</a> 
     <a class="link-to-listing" href="http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/">Se hele annoncen</a> 
    </div> 


<ul class="details"> 

    <li> 


<span>1.900 kr.</span> 
    </li> 

</ul> 

</td> 




       <td> 

<span>129</span></td> 
       <td> 

<span>9 dage</span></td> 
       <td>0</td> 
       <td> 

     <div class="listingUpselling bump-up-upsell"> 
      <p> 
       Er nu på side <b>57</b>. 
      </p> 
      <p><a href="/produkt-tilvalg/?id=1029751995&amp;produktId=BumpUp&amp;returnUrl=http%3A%2F%2Fwww.dba.dk%2Fmin-dbadk%2F" rel="nofollow" data-ga-act="bumpup-upsell-page-number-click" data-ga-lbl="tenzing-project" class="upsellingButton btn btn-small trackClicks">Køb oprykning</a></p> 
     </div> 

</td> 
<td class="noWrap myDbaActions"> 
    <ul class="unstyled"> 
      <li> 
       <a href="/rediger-annonce/?id=1029751995" data-ga-act="EditAdBegin" data-ga-lbl="" class="trackClicks">Rediger</a> 
      </li> 


      <li> 
       <a href="#" onclick="Dba.MyDba.MyListings.inactivateListing('/ajax/my-dba/MyDba/InActivateListing', 1029751995); return false; ">Deaktiver</a> 
      </li> 

     <li> 
      <a href="#" data-ga-act="DeleteAdBegin" data-ga-lbl="" onclick="Dba.MyDba.MyListings.deleteListing('/ajax/my-dba/MyDba/DeleteListing', 1029751995); return false;" class="trackClicks">Slet</a> 
     </li> 


      <li> 
       <a href="/paypal/anmodning/?externalid=1029751995" data-ga-act="click" data-ga-lbl="paypal-request-payment" class="trackClicks">Start PayPal</a> 
      </li> 

      <li> 
       <a href="/fragt/1029751995/" data-ga-act="shipping-buy-label" data-ga-lbl="shipping" class="trackClicks">Køb pakkelabel</a> 
      </li> 
    </ul> 
</td> 
       </tr> 


<tr class="dbaListing listing hasInsertionFee"> 

    <td class="pictureColumn" colspan="1"> 
     <div class="thumbnailContainer"> 
      <a class="thumbnailContainerInner" href="http://www.dba.dk/dykkerur-akribos-xxiv-test/id-1030155633/"> 

      <img class="thumbnail lazy image-placeholder" alt="Dykkerur, Akribos XXIV, test" data-original="http://dbastatic.dk/pictures/pictures/41/0d/7053-631b-4ba6-b520-cb0cbdd67b29.jpg?preset=srpgallery" src="http://dbastatic.dk/pictures/pictures/41/0d/7053-631b-4ba6-b520-cb0cbdd67b29.jpg?preset=srpgallery" style="display: block;"> 
      </a> 
     </div> 
    </td> 

<td class="mainContent" colspan="2"> 
    <script type="application/ld+json"> 
    { 
     "@context": "http://schema.org", 
     "@type": "Product", 
     "name": "Dykkerur, Akribos XXIV, test", 
     "image": "http://dbastatic.dk/pictures/pictures/41/0d/7053-631b-4ba6-b520-cb0cbdd67b29.jpg?preset=medium", 
     "url": "http://www.dba.dk/dykkerur-akribos-xxiv-test/id-1030155633/", 
     "offers": { 
      "@type": "Offer", 
      "priceCurrency": "DKK", 
      "price": "9999" 
     } 
    } 
    </script> 

    <a class="listingLink" href="http://www.dba.dk/dykkerur-akribos-xxiv-test/id-1030155633/"> 
      <span class="headline-block"> 
          </span> 

    </a> 

    <div class="expandable-box expandable-box-collapsed"> 
     <a class="listingLink" href="http://www.dba.dk/dykkerur-akribos-xxiv-test/id-1030155633/">Dykkerur, Akribos XXIV, test</a> 
     <a class="link-to-listing" href="http://www.dba.dk/dykkerur-akribos-xxiv-test/id-1030155633/">Se hele annoncen</a> 
    </div> 


<ul class="details"> 

    <li> 


<span>9.999 kr.</span> 
    </li> 

</ul> 

</td> 




       <td> 

<span>47</span></td> 
       <td> 

<span>29 dage</span></td> 
       <td>0</td> 
       <td> 

     <div class="listingUpselling bump-up-upsell"> 
      <p> 
       Er nu på side <b>8</b>. 
      </p> 
      <p><a href="/produkt-tilvalg/?id=1030155633&amp;produktId=BumpUp&amp;returnUrl=http%3A%2F%2Fwww.dba.dk%2Fmin-dbadk%2F" rel="nofollow" data-ga-act="bumpup-upsell-page-number-click" data-ga-lbl="tenzing-project" class="upsellingButton btn btn-small trackClicks">Køb oprykning</a></p> 
     </div> 

</td> 
<td class="noWrap myDbaActions"> 
    <ul class="unstyled"> 
      <li> 
       <a href="/rediger-annonce/?id=1030155633" data-ga-act="EditAdBegin" data-ga-lbl="" class="trackClicks">Rediger</a> 
      </li> 


      <li> 
       <a href="#" onclick="Dba.MyDba.MyListings.inactivateListing('/ajax/my-dba/MyDba/InActivateListing', 1030155633); return false; ">Deaktiver</a> 
      </li> 

     <li> 
      <a href="#" data-ga-act="DeleteAdBegin" data-ga-lbl="" onclick="Dba.MyDba.MyListings.deleteListing('/ajax/my-dba/MyDba/DeleteListing', 1030155633); return false;" class="trackClicks">Slet</a> 
     </li> 


      <li> 
       <a href="/paypal/anmodning/?externalid=1030155633" data-ga-act="click" data-ga-lbl="paypal-request-payment" class="trackClicks">Start PayPal</a> 
      </li> 

      <li> 
       <a href="/fragt/1030155633/" data-ga-act="shipping-buy-label" data-ga-lbl="shipping" class="trackClicks">Køb pakkelabel</a> 
      </li> 
    </ul> 
</td> 
       </tr> 


<tr class="dbaListing listing hasInsertionFee"> 

    <td class="pictureColumn" colspan="1"> 
     <div class="thumbnailContainer"> 
      <a class="thumbnailContainerInner" href="http://www.dba.dk/dykkerur-adidas-test/id-1030155766/"> 

      <img class="thumbnail lazy image-placeholder" alt="Dykkerur, Adidas, test" data-original="http://dbastatic.dk/pictures/pictures/6d/45/bee3-8bb7-4e0b-9cb4-4c75ebea7ac2.jpg?preset=srpgallery" src="http://dbastatic.dk/pictures/pictures/6d/45/bee3-8bb7-4e0b-9cb4-4c75ebea7ac2.jpg?preset=srpgallery" style="display: block;"> 
      </a> 
     </div> 
    </td> 

<td class="mainContent" colspan="2"> 
    <script type="application/ld+json"> 
    { 
     "@context": "http://schema.org", 
     "@type": "Product", 
     "name": "Dykkerur, Adidas, test", 
     "image": "http://dbastatic.dk/pictures/pictures/6d/45/bee3-8bb7-4e0b-9cb4-4c75ebea7ac2.jpg?preset=medium", 
     "url": "http://www.dba.dk/dykkerur-adidas-test/id-1030155766/", 
     "offers": { 
      "@type": "Offer", 
      "priceCurrency": "DKK", 
      "price": "9999" 
     } 
    } 
    </script> 

    <a class="listingLink" href="http://www.dba.dk/dykkerur-adidas-test/id-1030155766/"> 
      <span class="headline-block"> 
          </span> 

    </a> 

    <div class="expandable-box expandable-box-collapsed"> 
     <a class="listingLink" href="http://www.dba.dk/dykkerur-adidas-test/id-1030155766/">Dykkerur, Adidas, test</a> 
     <a class="link-to-listing" href="http://www.dba.dk/dykkerur-adidas-test/id-1030155766/">Se hele annoncen</a> 
    </div> 


<ul class="details"> 

    <li> 


<span>9.999 kr.</span> 
    </li> 

</ul> 

</td> 




       <td> 

<span>51</span></td> 
       <td> 

<span>29 dage</span></td> 
       <td>0</td> 
       <td> 

     <div class="listingUpselling bump-up-upsell"> 
      <p> 
       Er nu på side <b>8</b>. 
      </p> 
      <p><a href="/produkt-tilvalg/?id=1030155766&amp;produktId=BumpUp&amp;returnUrl=http%3A%2F%2Fwww.dba.dk%2Fmin-dbadk%2F" rel="nofollow" data-ga-act="bumpup-upsell-page-number-click" data-ga-lbl="tenzing-project" class="upsellingButton btn btn-small trackClicks">Køb oprykning</a></p> 
     </div> 

</td> 
<td class="noWrap myDbaActions"> 
    <ul class="unstyled"> 
      <li> 
       <a href="/rediger-annonce/?id=1030155766" data-ga-act="EditAdBegin" data-ga-lbl="" class="trackClicks">Rediger</a> 
      </li> 


      <li> 
       <a href="#" onclick="Dba.MyDba.MyListings.inactivateListing('/ajax/my-dba/MyDba/InActivateListing', 1030155766); return false; ">Deaktiver</a> 
      </li> 

     <li> 
      <a href="#" data-ga-act="DeleteAdBegin" data-ga-lbl="" onclick="Dba.MyDba.MyListings.deleteListing('/ajax/my-dba/MyDba/DeleteListing', 1030155766); return false;" class="trackClicks">Slet</a> 
     </li> 


      <li> 
       <a href="/paypal/anmodning/?externalid=1030155766" data-ga-act="click" data-ga-lbl="paypal-request-payment" class="trackClicks">Start PayPal</a> 
      </li> 

      <li> 
       <a href="/fragt/1030155766/" data-ga-act="shipping-buy-label" data-ga-lbl="shipping" class="trackClicks">Køb pakkelabel</a> 
      </li> 
    </ul> 
</td> 
       </tr> 


<tr class="dbaListing listing lastListing hasInsertionFee"> 

    <td class="pictureColumn" colspan="1"> 
     <div class="thumbnailContainer"> 
      <a class="thumbnailContainerInner" href="http://www.dba.dk/joystick-l-h-defekt-test/id-1030156328/"> 

      <img class="thumbnail lazy image-placeholder" alt="Joystick, l, h, Defekt, test" data-original="http://dbastatic.dk/pictures/pictures/25/48/8637-94be-42b8-a809-ed69822a40e4.jpg?preset=srpgallery" src="http://dbastatic.dk/pictures/pictures/25/48/8637-94be-42b8-a809-ed69822a40e4.jpg?preset=srpgallery" style="display: block;"> 
      </a> 
     </div> 
    </td> 

<td class="mainContent" colspan="2"> 
    <script type="application/ld+json"> 
    { 
     "@context": "http://schema.org", 
     "@type": "Product", 
     "name": "Joystick, l, h, Defekt, test", 
     "image": "http://dbastatic.dk/pictures/pictures/25/48/8637-94be-42b8-a809-ed69822a40e4.jpg?preset=medium", 
     "url": "http://www.dba.dk/joystick-l-h-defekt-test/id-1030156328/", 
     "offers": { 
      "@type": "Offer", 
      "priceCurrency": "DKK", 
      "price": "900" 
     } 
    } 
    </script> 

    <a class="listingLink" href="http://www.dba.dk/joystick-l-h-defekt-test/id-1030156328/"> 
      <span class="headline-block"> 
          </span> 

    </a> 

    <div class="expandable-box expandable-box-collapsed"> 
     <a class="listingLink" href="http://www.dba.dk/joystick-l-h-defekt-test/id-1030156328/">Joystick, l, h, Defekt, test</a> 
     <a class="link-to-listing" href="http://www.dba.dk/joystick-l-h-defekt-test/id-1030156328/">Se hele annoncen</a> 
    </div> 


<ul class="details"> 

    <li> 


<span>900 kr.</span> 
    </li> 

</ul> 

</td> 




       <td> 

<span>44</span></td> 
       <td> 

<span>29 dage</span></td> 
       <td>0</td> 
       <td> 

     <div class="listingUpselling bump-up-upsell"> 
      <p> 
       Er nu på side <b>5</b>. 
      </p> 
      <p><a href="/produkt-tilvalg/?id=1030156328&amp;produktId=BumpUp&amp;returnUrl=http%3A%2F%2Fwww.dba.dk%2Fmin-dbadk%2F" rel="nofollow" data-ga-act="bumpup-upsell-page-number-click" data-ga-lbl="tenzing-project" class="upsellingButton btn btn-small trackClicks">Køb oprykning</a></p> 
     </div> 

</td> 
<td class="noWrap myDbaActions"> 
    <ul class="unstyled"> 
      <li> 
       <a href="/rediger-annonce/?id=1030156328" data-ga-act="EditAdBegin" data-ga-lbl="" class="trackClicks">Rediger</a> 
      </li> 


      <li> 
       <a href="#" onclick="Dba.MyDba.MyListings.inactivateListing('/ajax/my-dba/MyDba/InActivateListing', 1030156328); return false; ">Deaktiver</a> 
      </li> 

     <li> 
      <a href="#" data-ga-act="DeleteAdBegin" data-ga-lbl="" onclick="Dba.MyDba.MyListings.deleteListing('/ajax/my-dba/MyDba/DeleteListing', 1030156328); return false;" class="trackClicks">Slet</a> 
     </li> 


      <li> 
       <a href="/paypal/anmodning/?externalid=1030156328" data-ga-act="click" data-ga-lbl="paypal-request-payment" class="trackClicks">Start PayPal</a> 
      </li> 

      <li> 
       <a href="/fragt/1030156328/" data-ga-act="shipping-buy-label" data-ga-lbl="shipping" class="trackClicks">Køb pakkelabel</a> 
      </li> 
    </ul> 
</td> 
       </tr> 

     </tbody> 
+0

何が含まれていません印刷物の結果ですか? –

答えて

0

間違ったセレクタを使用しているようです。以下試してみてください。<td class="mainContent">の直接の子にあなたのセレクタtd.mainContent > aポイント

elem = browser.find_element_by_css_selector('div.expandable-box.expandable-box-collapsed > a.listingLink') 
print(elem.text) 

UPDATE

とあなたが見ることができるように、これは

<a class="listingLink" href="http://www.dba.dk/tastatur-mad-catz-strike-7/id-1029751995/"> 
     <span class="headline-block"></span> 
</a> 

あり、それはテキストノード

+0

それは働いた!あなたは男ですが、私は答えを得て理解していないのが好きです。 あなたはその結論に至った経緯を説明してください。あなたは「緑色の解決済みの小切手」を手に入れることができますか? –

+0

更新された回答を確認する – Andersson

0

この

を試してみてください:htmlコードが

(私は奇妙である..タフなエラーを取得していません)

elem = browser.find_element_by_xpath('//td[@class='mainContent']/a') 
print(elem.text) 
+0

私は仕事をしなかったし、ええ、「mainContent」を「mainContent」に変更しました 他のアイデアはありますか? –

関連する問題