简体   繁体   English

如何在python中获取td标签内链接的某些部分

[英]How to grab some part of the link inside the td tag in python

I'm trying to grab the link inside a td.我正在尝试获取 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

Hope, it will work.希望,它会起作用。 try this:尝试这个:

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

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', ...) .最好在当前选择的<td>搜索<a>元素而不是在整个文档中搜索,所以我将代码更改为td = row.find_all('td')[8] ,然后更改为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM