情似当年
2020-09-18 10:38
采纳率: 33.3%
浏览 88

XPATH在python selenium中的定位当前节点的子点的问题

RT 我想定位一个 id="middle" 下的所有class="result"的节点,并获取当前这个节点下的字节点下的class="resultsimilarityinfo"如果resultsimilarityinfo的.text大于90%,则取这个result下的一个URL。
问题出在在定位resultsimilarityinfo时并不是在当前定位到的result节点下进行的,而是把其他所有result的resultsimilarityinfo也给返回了。求教如何定位当前节点的字节点而不考虑他们的位置(但是不能处于当前节点之外!现在问题就出在其他地方的字节点也被匹配出来了!)

其实这个网站是一个以图搜图的网站,每次用手点我觉得麻烦,所以想自动化这个事情。但是匹配的结果未必是完全正确的,所以我想根据resultsimilarityinfo中的相似度进行判断,如果判断到结果大于90%,就把当前result的URL或者Creator记录下来。求大佬帮忙!

from selenium import webdriver
import time

# 设置 :忽略正在受自动测试软件的控制
option = webdriver.ChromeOptions()
option.add_argument('disable-infobars')
# 实例化 对象
driver = webdriver.Chrome(executable_path="/Users/dasouche/Downloads/chromedriver")
# 打开网址
driver.get("https://saucenao.com/index.php")
time.sleep(5)
file = driver.find_element_by_name('file')
time.sleep(1)
file.send_keys()
time.sleep(1)
driver.find_element_by_xpath('//input[@type="submit" and @value="get sauce"]').click()
time.sleep(5)

results = driver.find_elements_by_xpath('//div[@class="result"]')
for result in results:
    resultsimilarityinfo = result.find_elements_by_xpath('//div[@class="resultsimilarityinfo"]')

time.sleep(1)
driver.close()

<html>
 <head></head>
 <body>
  <div id="middle"> 
   <div id="smalllogo">
    <img alt="" src="images/static/bannersmall.png" />
   </div> 
   <div class="result">
    <table class="resulttable">
     <tbody>
      <tr>
       <td class="resulttableimage">
        <div class="resultimage">
         <a href="https://saucenao.com/search.php?db=999&amp;url=https%3A%2F%2Fimg1.saucenao.com%2Fres%2Fpixiv%2F8043%2F80433091_p0_master1200.jpg%3Fauth%3DspiQGa3wHjCqW0R2w5uFIQ%26exp%3D1600398644" class="linkify"><img title="Index #5: Pixiv Images - 80433091_p0_master1200.jpg" raw-rating="1" src="https://img1.saucenao.com/res/pixiv/8043/80433091_p0_master1200.jpg?auth=spiQGa3wHjCqW0R2w5uFIQ&amp;exp=1600398644" border="0" /></a>
        </div></td>
       <td class="resulttablecontent">
        <div class="resultmatchinfo">
         <div class="resultsimilarityinfo">
          95.60%
         </div>
         <div class="resultmiscinfo"></div>
        </div>
        <div class="resultcontent">
         <div class="resulttitle">
          <strong>女子高生</strong>
          <br />
         </div>
         <div class="resultcontentcolumn">
          <strong>Pixiv ID: </strong>
          <a href="https://www.pixiv.net/member_illust.php?mode=medium&amp;illust_id=80433091" class="linkify">80433091</a>
          <span style="padding-left:10px;"><a href="https://saucenao.com/info.php?lookup_type=0&amp;db=5&amp;id=80433091"><img src="/images/static/saucenao_info.png" height="10px" width="10px" /></a></span>
          <br />
          <strong>Member: </strong>
          <a href="https://www.pixiv.net/member.php?id=13765263" class="linkify">バラ</a>
          <span style="padding-left:10px;"><a href="https://saucenao.com/info.php?lookup_type=1&amp;db=5&amp;id=13765263"><img src="/images/static/saucenao_info.png" height="10px" width="10px" /></a></span>
          <br />
          <br />
         </div>
        </div></td>
      </tr>
     </tbody>
    </table>
   </div>
   <div class="result">
    <table class="resulttable">
     <tbody>
      <tr>
       <td class="resulttableimage">
        <div class="resultimage">
         <a href="https://saucenao.com/search.php?db=999&amp;url=https%3A%2F%2Fimg1.saucenao.com%2Fres%2Fpixiv%2F8098%2F80983819_p0_master1200.jpg%3Fauth%3D5GtoTV6EXDL69YELV8pJ7g%26exp%3D1600398644" class="linkify"><img title="Index #5: Pixiv Images - 80983819_p0_master1200.jpg" raw-rating="1" src="https://img1.saucenao.com/res/pixiv/8098/80983819_p0_master1200.jpg?auth=5GtoTV6EXDL69YELV8pJ7g&amp;exp=1600398644" border="0" /></a>
        </div></td>
       <td class="resulttablecontent">
        <div class="resultmatchinfo">
         <div class="resultsimilarityinfo">
          94.81%
         </div>
         <div class="resultmiscinfo"></div>
        </div>
        <div class="resultcontent">
         <div class="resulttitle">
          <strong>High School Girl</strong>
          <br />
         </div>
         <div class="resultcontentcolumn">
          <strong>Pixiv ID: </strong>
          <a href="https://www.pixiv.net/member_illust.php?mode=medium&amp;illust_id=80983819" class="linkify">80983819</a>
          <span style="padding-left:10px;"><a href="https://saucenao.com/info.php?lookup_type=0&amp;db=5&amp;id=80983819"><img src="/images/static/saucenao_info.png" height="10px" width="10px" /></a></span>
          <br />
          <strong>Member: </strong>
          <a href="https://www.pixiv.net/member.php?id=31349507" class="linkify">Skylez</a>
          <span style="padding-left:10px;"><a href="https://saucenao.com/info.php?lookup_type=1&amp;db=5&amp;id=31349507"><img src="/images/static/saucenao_info.png" height="10px" width="10px" /></a></span>
          <br />
          <br />
         </div>
        </div></td>
      </tr>
     </tbody>
    </table>
   </div>
   <div class="result">
    <table class="resulttable">
     <tbody>
      <tr>
       <td class="resulttableimage">
        <div class="resultimage">
         <a href="https://saucenao.com/search.php?db=999&amp;url=https%3A%2F%2Fimg3.saucenao.com%2Fbooru%2Fb%2F2%2Fb2ddcba1e63a2400c6e48dd11fe09499_1.jpg" class="linkify"><img title="Index #12: Yande.re - b2ddcba1e63a2400c6e48dd11fe09499_1.jpg" raw-rating="1" src="https://img3.saucenao.com/booru/b/2/b2ddcba1e63a2400c6e48dd11fe09499_1.jpg" border="0" /></a>
        </div></td>
       <td class="resulttablecontent">
        <div class="resultmatchinfo">
         <div class="resultsimilarityinfo">
          95.73%
         </div>
         <div class="resultmiscinfo">
          <a href="https://danbooru.donmai.us/post/show/3874932"><img src="images/static/siteicons/danbooru.ico" width="16" height="16" border="0" alt="" /></a>
          <br />
          <br style="line-height:2px;" />
          <a href="https://yande.re/post/show/626407"><img src="images/static/siteicons/yandere.ico" width="16" height="16" border="0" alt="" /></a>
          <br />
          <br style="line-height:2px;" />
          <a href="https://gelbooru.com/index.php?page=post&amp;s=view&amp;id=5253538"><img src="images/static/siteicons/gelbooru.ico" width="16" height="16" border="0" alt="" /></a>
          <br />
          <br style="line-height:2px;" />
         </div>
        </div>
        <div class="resultcontent">
         <div class="resulttitle">
          <strong>Creator: </strong>bara0717
          <br />
         </div>
         <div class="resultcontentcolumn">
          <strong>Source: </strong>
          <a href="https://www.pixiv.net/member_illust.php?mode=medium&amp;illust_id=80433091">Pixiv #80433091</a>
          <br />
          <strong>Material: </strong>original
          <br />
         </div>
         <div class="resultcontentcolumn"></div>
        </div></td>
      </tr>
     </tbody>
    </table>
   </div>
   <div class="result" id="result-hidden-notification" onclick="showHidden()">
    Low similarity results have been hidden. Click here to display them...
   </div>
   <div class="result hidden">
    <table class="resulttable">
     <tbody>
      <tr>
       <td class="resulttableimage">
        <div class="resultimage">
         <a href="https://saucenao.com/search.php?db=999&amp;url=https%3A%2F%2Fimg1.saucenao.com%2Fres%2Fpixiv%2F1916%2Fmanga%2F19165192_p4.jpg%3Fauth%3D7WyI6HE5ec2TO1XMo6hJNw%26exp%3D1600398644" class="linkify"><img title="Index #5: Pixiv Images - 19165192_p4.jpg" raw-rating="0" src="images/static/blocked.gif" data-dly="1" data-src="https://img1.saucenao.com/res/pixiv/1916/manga/19165192_p4.jpg?auth=7WyI6HE5ec2TO1XMo6hJNw&amp;exp=1600398644" border="0" /></a>
        </div></td>
       <td class="resulttablecontent">
        <div class="resultmatchinfo">
         <div class="resultsimilarityinfo">
          50.99%
         </div>
         <div class="resultmiscinfo"></div>
        </div>
        <div class="resultcontent">
         <div class="resulttitle">
          <strong>かまってちゃんに負けた</strong>
          <br />
         </div>
         <div class="resultcontentcolumn">
          <strong>Pixiv ID: </strong>
          <a href="https://www.pixiv.net/member_illust.php?mode=medium&amp;illust_id=19165192" class="linkify">19165192</a>
          <span style="padding-left:10px;"><a href="https://saucenao.com/info.php?lookup_type=0&amp;db=5&amp;id=19165192"><img src="/images/static/saucenao_info.png" height="10px" width="10px" /></a></span>
          <br />
          <strong>Member: </strong>
          <a href="https://www.pixiv.net/member.php?id=2256198" class="linkify">おしみ</a>
          <span style="padding-left:10px;"><a href="https://saucenao.com/info.php?lookup_type=1&amp;db=5&amp;id=2256198"><img src="/images/static/saucenao_info.png" height="10px" width="10px" /></a></span>
          <br />
          <br />
         </div>
        </div></td>
      </tr>
     </tbody>
    </table>
   </div>
   <div class="result hidden">
    <table class="resulttable">
     <tbody>
      <tr>
       <td class="resulttableimage">
        <div class="resultimage">
         <a href="https://saucenao.com/search.php?db=999&amp;url=https%3A%2F%2Fimg3.saucenao.com%2FdA2%2F82111%2F821111177.jpg" class="linkify"><img title="Index #34: deviantArt2 - 821111177.jpg" raw-rating="1" src="images/static/blocked.gif" data-dly="1" data-src="https://img3.saucenao.com/dA2/82111/821111177.jpg" border="0" /></a>
        </div></td>
       <td class="resulttablecontent">
        <div class="resultmatchinfo">
         <div class="resultsimilarityinfo">
          50.35%
         </div>
         <div class="resultmiscinfo"></div>
        </div>
        <div class="resultcontent">
         <div class="resulttitle">
          <strong>Digimonverse AU - OC</strong>
          <br />
         </div>
         <div class="resultcontentcolumn">
          <strong>dA ID: </strong>
          <a href="https://deviantart.com/view/821111177" class="linkify">821111177</a>
          <br />
          <strong>Author: </strong>
          <a href="https://www.deviantart.com/aisurori" class="linkify">aisurori</a>
          <br />
          <br />
         </div>
        </div></td>
      </tr>
     </tbody>
    </table>
   </div>
   <div class="result hidden">
    <table class="resulttable">
     <tbody>
      <tr>
       <td class="resulttableimage">
        <div class="resultimage">
         <a href="https://saucenao.com/search.php?db=999&amp;url=https%3A%2F%2Fimg3.saucenao.com%2Fbooru%2F1%2F1%2F118a4a4e12b283336a564ae98f6663ad_4.jpg" class="linkify"><img title="Index #27: Sankaku Channel - 118a4a4e12b283336a564ae98f6663ad_4.jpg" raw-rating="1" src="images/static/blocked.gif" data-dly="1" data-src="https://img3.saucenao.com/booru/1/1/118a4a4e12b283336a564ae98f6663ad_4.jpg" border="0" /></a>
        </div></td>
       <td class="resulttablecontent">
        <div class="resultmatchinfo">
         <div class="resultsimilarityinfo">
          50.05%
         </div>
         <div class="resultmiscinfo">
          <a href="https://chan.sankakucomplex.com/post/show/5628372"><img src="images/static/siteicons/sankaku.ico" width="16" height="16" border="0" alt="" /></a>
          <br />
          <br style="line-height:2px;" />
         </div>
        </div>
        <div class="resultcontent">
         <div class="resulttitle">
          <strong>Creator: </strong>oikawa (pixiv 6202749)
          <br />
         </div>
         <div class="resultcontentcolumn">
          <strong>Material: </strong>pokemon (manga)
          <br />pokemon adventures
          <br />pokemon special
          <br />pokemon
          <br />
         </div>
         <div class="resultcontentcolumn">
          <strong>Characters: </strong>
          <br />pokemon character
          <br />calem (pokemon)
          <br />yvonne gabena
          <br />xavier (pokemon)
          <br />serena (pokemon)
          <br />protagonist (pokemon)
          <br />
         </div>
        </div></td>
      </tr>
     </tbody>
    </table>
   </div> 
   <br /> 
   <div style="text-align:center; margin-top:15px;"> 
    <!-- /21687543875/1425438 --> 
    <div id="1425438" data-google-query-id="CIip-5HQ8esCFRfHfAodKLECXg"> 
     <div id="google_ads_iframe_/21687543875/1425438_0__container__" style="border: 0pt none;">
      <iframe id="google_ads_iframe_/21687543875/1425438_0" title="3rd party ad content" name="google_ads_iframe_/21687543875/1425438_0" width="728" height="90" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" srcdoc="" data-google-container-id="1" style="border: 0px; vertical-align: bottom;" data-load-complete="true"></iframe>
     </div>
    </div> 
   </div> 
  </div>
 </body>
</html>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题