Hack Forums
[Python] xpath question - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] xpath question (/showthread.php?tid=5638993)



xpath question - Skidy - Jun 4, 2017

PHP Code

<?php 
<tr >
              <
td><a href="URL">VTC</a><br/>I WANT THIS</td>
              <
td class="paragraph not-important">2017-06-04 12:34</td>
              <
td class="paragraph">0.00019500</td>

                <
td class="grey not-important">
                  
0.00019752
                  
<br/>

                    <
span class="green diff">+1.29%</span>
                </
td>
                <
td class="grey not-important">
                  
0.00019752
                  
<br/>

                    <
span class="green diff">+1.29%</span>
                </
td>
                <
td class="grey ">
                  
0.00019752
                  
<br/>

                    <
span class="green diff">+1.29%</span>
                </
td>
            </
tr> 

I am doing
PHP Code

<?php 
tree
.xpath("//tr/td/text()") 

However I am getting all the numbers and everything. I just need the text "I WANT THIS" after the </br>. The way I am doing it now gives me everything. Note that there are loads of tr. I have just copied one here to make it easier to see.


* - Vezel - Jun 4, 2017

(Jun 4, 2017 06:14 AM)Skidy Wrote:  
PHP Code

<?php 
<tr >
              <
td><a href="URL">VTC</a><br/>I WANT THIS</td>
              <
td class="paragraph not-important">2017-06-04 12:34</td>
              <
td class="paragraph">0.00019500</td>

                <
td class="grey not-important">
                  
0.00019752
                  
<br/>

                    <
span class="green diff">+1.29%</span>
                </
td>
                <
td class="grey not-important">
                  
0.00019752
                  
<br/>

                    <
span class="green diff">+1.29%</span>
                </
td>
                <
td class="grey ">
                  
0.00019752
                  
<br/>

                    <
span class="green diff">+1.29%</span>
                </
td>
            </
tr> 

I am doing
PHP Code

<?php 
tree
.xpath("//tr/td/text()") 

However I am getting all the numbers and everything. I just need the text "I WANT THIS" after the </br>. The way I am doing it now gives me everything. Note that there are loads of tr. I have just copied one here to make it easier to see.

Check out "BeautifulSoup" for python, then you can do kinda like this
Code
soup = BeautifulSoup(c.get("URL"), headers=headers).content, "lxml")
    tds = soup.findAll("td")
    for td in tds:
        l = BeautifulSoup(str(td), "lxml")
        l.find("a").next // Should get the element after the a



* - Skidy - Jun 4, 2017

(Jun 4, 2017 06:18 AM)Vezel Wrote:  Check out "BeautifulSoup" for python, then you can do kinda like this
Code
soup = BeautifulSoup(c.get("URL"), headers=headers).content, "lxml")
    tds = soup.findAll("td")
    for td in tds:
        l = BeautifulSoup(str(td), "lxml")
        l.find("a").next // Should get the element after the a

Thank you I fiddled with your code as it didn't originally work but now does.

Still would like it just a 1 liner.
PHP Code

<?php 
crypto_platform 
= tree.xpath("//tr/td/text()[preceding-sibling::br]") 

Its a temp solution :)


* - Vezel - Jun 4, 2017

(Jun 4, 2017 09:19 AM)Skidy Wrote:  Thank you I fiddled with your code as it didn't originally work but now does.

Still would like it just a 1 liner.
PHP Code

<?php 
crypto_platform 
= tree.xpath("//tr/td/text()[preceding-sibling::br]") 

Its a temp solution :)

No problem, yeah I didn't test the code. Just wrote from the top of my head :)

Glad you got it to work!