Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从HTML表中获取href链接的策略_Javascript_Python_Html_Selenium_Xpath - Fatal编程技术网

Javascript 从HTML表中获取href链接的策略

Javascript 从HTML表中获取href链接的策略,javascript,python,html,selenium,xpath,Javascript,Python,Html,Selenium,Xpath,我的网络搜索返回不同数量的表行,我不习惯处理表。这些表各有3列。我检查第一列的街道号码是否正确。如果是,那么我需要从该行的第三列中获取链接。有什么想法吗?我的代码显然不起作用,但应该给出我正在尝试做什么的想法。我的代码目前不逐行处理数据,只处理td-by-td 我理想的代码是什么样子的: 抓取第一个表行 检查第一列中的街道是否匹配 如果#匹配,则获取第三列中的链接 如果#不匹配,则移动到检查下一个表行 我对编程仍然非常陌生,所以我的代码仍然是笨重的简单化(但写得很糟糕)的 有问题的HTML &l

我的网络搜索返回不同数量的表行,我不习惯处理表。这些表各有3列。我检查第一列的街道号码是否正确。如果是,那么我需要从该行的第三列中获取链接。有什么想法吗?我的代码显然不起作用,但应该给出我正在尝试做什么的想法。我的代码目前不逐行处理数据,只处理td-by-td

我理想的代码是什么样子的:

  • 抓取第一个表行

  • 检查第一列中的街道是否匹配

  • 如果#匹配,则获取第三列中的链接

  • 如果#不匹配,则移动到检查下一个表行

  • 我对编程仍然非常陌生,所以我的代码仍然是笨重的简单化(但写得很糟糕)的

    有问题的HTML

    <tr>
      <td class="transactionTableBodyText">5250 S RAINBOW BLVD 1001  </td>
      <td class="transactionTableBodyText" align="center">SPRING VALLEY</td>
      <td class="transactionTableBodyText" align="center">
        <a id="DataGrid1__ctl3_HyperLink2" href="http://Sandgate.co.clark.nv.us/AssrRealProp/ParcelDetail.aspx?hdnParcel=16326214001&amp;hdnInstance=pcl7" target="_self">163-26-214-001</a>
      </td>
    </tr>
    
    
    彩虹大道南5250号1001
    泉谷
    
    我的pythonicselenium代码

    td = driver.find_elements_by_css_selector("td.transactionTableBodyText")
        i = 0
        while i <= len(td):
            try:
                if StrtNum in td[i].text:
                    print("We matched %s!" % (StrtNum))
    
                    #This is the incorrect part where I try to get to the 'a' selector in the 3rd column from the correctly identified 1st column.
                    ParcelLink = td[i+2].driver.find_elements_by_css_selector('a').get_attribute('href')
                    print(ParcelLink)
                else:
                    return
            except:
                break
            i+=1
    
    td=driver.通过css选择器(“td.transactionTableBodyText”)查找元素
    i=0
    
    而我为你的弓准备了另一根弦:lxml。它使您能够访问xpath表达式,从而简化HTML导航工作

    >>> from lxml import etree
    >>> parser = etree.HTMLParser()
    >>> tree = etree.fromstring(open('sample.htm').read(), parser)
    >>> for td in tree.xpath('.//table/tr/td[@class="transactionTableBodyText"][1]'):
    ...     if td.text.startswith('5250'):
    ...         tr = td.getparent()
    ...         td_3_link = tr.xpath('./td[3]/a')
    ...         link = td_3_link[0].attrib['href']
    ... 
    >>> link
    'http://Sandgate.co.clark.nv.us/AssrRealProp/ParcelDetail.aspx?hdnParcel=16326214001&hdnInstance=pcl7'
    
    正如您所看到的,我使用了REPL环境,这样我可以尝试各种方法,同时尝试收敛到一些运行良好且看起来相当健壮的方法

    差点忘了,这是我正在解析的HTML

    <table>
        <tr>
          <td class="transactionTableBodyText">1000 S RAINBOW BLVD 1001  </td>
          <td class="transactionTableBodyText" align="center">SPRING VALLEY</td>
          <td class="transactionTableBodyText" align="center">
            <a id="DataGrid1__ctl3_HyperLink2" href="http://Sandgate.co.clark.nv.us/AssrRealProp/ParcelDetail.aspx?hdnParcel=16326214001&amp;hdnInstance=pcl7" target="_self">163-26-214-001</a>
          </td>
        </tr>
        <tr>
          <td class="transactionTableBodyText">5250 S RAINBOW BLVD 1001  </td>
          <td class="transactionTableBodyText" align="center">SPRING VALLEY</td>
          <td class="transactionTableBodyText" align="center">
            <a id="DataGrid1__ctl3_HyperLink2" href="http://Sandgate.co.clark.nv.us/AssrRealProp/ParcelDetail.aspx?hdnParcel=16326214001&amp;hdnInstance=pcl7" target="_self">163-26-214-001</a>
          </td>
        </tr>
        <tr>
          <td class="transactionTableBodyText">2000 S RAINBOW BLVD 1001  </td>
          <td class="transactionTableBodyText" align="center">SPRING VALLEY</td>
          <td class="transactionTableBodyText" align="center">
            <a id="DataGrid1__ctl3_HyperLink2" href="http://Sandgate.co.clark.nv.us/AssrRealProp/ParcelDetail.aspx?hdnParcel=16326214001&amp;hdnInstance=pcl7" target="_self">163-26-214-001</a>
          </td>
        </tr>
        <tr>
          <td class="transactionTableBodyText">2000 S RAINBOW BLVD 1001  </td>
          <td class="transactionTableBodyText" align="center">SPRING VALLEY</td>
          <td class="transactionTableBodyText" align="center">
            <a id="DataGrid1__ctl3_HyperLink2" href="http://Sandgate.co.clark.nv.us/AssrRealProp/ParcelDetail.aspx?hdnParcel=16326214001&amp;hdnInstance=pcl7" target="_self">163-26-214-001</a>
          </td>
        </tr>
    </table>'''
    
    
    彩虹大道1000号1001
    泉谷
    彩虹大道南5250号1001
    泉谷
    2000年彩虹大道1001号
    泉谷
    2000年彩虹大道1001号
    泉谷
    '''
    
    看一看漂亮的汤我最初的大部分网页抓取实验都是用BS4完成的。我一直在使用Selenium,因为在讨论中的页面上大量使用JS。我将看看是否可以使用BS4来处理我正在努力解决的问题。谢谢汉克比尔,我来看看这个。我知道我也必须为未来的项目建立一个更健壮/不那么脆弱的系统。