Home > Software design >  How to grab some part of the link inside the td tag in python
How to grab some part of the link inside the td tag in python

Time:11-03

I'm trying to grab the link inside a td. My code does not display the link or produce the desired output. What I need to change.

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
from time import sleep
import requests

headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:92.0) Gecko/20100101 Firefox/92.0"}
urllink = "https://bscscan.com/txs?block=11711353&ps=100&p=1"

reqblockdetails = requests.get(urllink, headers=headers, timeout=5)
soupblockdetails = BeautifulSoup(reqblockdetails.content, 'html.parser')
rowsblockdetails = soupblockdetails.findAll('table')[0].findAll('tr')
sleep(1)
for row in rowsblockdetails[1:]:
    txnhash = row.find_all('td')[1].text[0:]
    txnhashdetails = txnhash.strip()
    destination = row.find_all('td')[8].text[0:]
    destination = destination.strip()    
    if str(destination) == "CoinOne: CONE Token":
        urldest = soupblockdetails.find('a', attrs={'class': 'hash-tag text-truncate'}).text
        print (" {:>1}  {:<5}".format(txnhashdetails, destination))
        print (urldest)
    else:
        pass

Current Output:

0x8265a6ba5ce531df645b883e8735af57241f43e92eb3c9a88f43b89310f964bc  CoinOne: CONE Token  Validator: Stake2me

Needed Output:

0x8265a6ba5ce531df645b883e8735af57241f43e92eb3c9a88f43b89310f964bc  CoinOne: CONE Token   0x9628735017f1a985ebaac0b203efb9e8d3ed0fef

CodePudding user response:

It would be better to search for <a> element in currently selected <td> but not in whole document so I changed code to td = row.find_all('td')[8] and later to td.find('a', ...).

Here is a working code:

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
from time import sleep
import requests

headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:92.0) Gecko/20100101 Firefox/92.0"}
urllink = "https://bscscan.com/txs?block=11711353&ps=100&p=1"

reqblockdetails = requests.get(urllink, headers=headers, timeout=5)
soupblockdetails = BeautifulSoup(reqblockdetails.content, 'html.parser')
rowsblockdetails = soupblockdetails.findAll('table')[0].findAll('tr')
sleep(1)
for row in rowsblockdetails[1:]:
    txnhash = row.find_all('td')[1].text[0:]
    txnhashdetails = txnhash.strip()
    td = row.find_all('td')[8]
    destination = td.text[0:].strip()    
    if str(destination) == "CoinOne: CONE Token":
        urldest = td.find('a', attrs={'class': 'hash-tag text-truncate'})["href"].lstrip("/address/")
        print (" {:>1}  {:<5}".format(txnhashdetails, destination))
        print (urldest)
    else:
        pass

CodePudding user response:

Hope, it will work. try this:

t_link = soupblockdetails.find('span', attrs={'class': 'hash-tag text-truncate'})
urldest = t_link.a['href']
  • Related