I have this data as you can see :
[<td><span></span></td>, <td><span></span></td>, <td><a class="cmc-link" href="/currencies/renbtc/"><span class="circle"></span><span>renBTC</span><span class="crypto-symbol">RENBTC</span></a></td>, <td><span>$<!-- -->61947.68</span></td>, <td><span></span></td>]
I want to extract the href link as you can see here is /currencies/renbtc/.
Here is my code:
from bs4 import BeautifulSoup
import requests
try:
r = requests.get('https://coinmarketcap.com/')
soup = BeautifulSoup(r.text, 'lxml')
table = soup.find('table', class_='cmc-table')
for row in table.tbody.find_all('tr'):
# Find all data for each column
columns = row.find_all('td')
print(columns)
except requests.exceptions.RequestException as e:
print(e)
but the result is the whole columns .
CodePudding user response:
Iterate over the <td> in the list and if that <td> has <a> (if td.a), then .get('href') of td.a:
from bs4 import BeautifulSoup
import requests
try:
r = requests.get('https://coinmarketcap.com/')
soup = BeautifulSoup(r.text, 'lxml')
table = soup.find('table', class_='cmc-table')
for row in table.tbody.find_all('tr'):
# Find all data for each column
columns = row.find_all('td')
for td in columns:
if td.a:
print(td.a.get('href'))
# theoretically for performance you can
# break
# here to stop the loop if you expect only one anchor link per `td`
except requests.exceptions.RequestException as e:
print(e)
CodePudding user response:
Operate on the element from columns that holds the <a>, select it and get its href:
link = columns[2].a['href']
Example
from bs4 import BeautifulSoup
import requests
try:
r = requests.get('https://coinmarketcap.com/')
soup = BeautifulSoup(r.text, 'lxml')
table = soup.find('table', class_='cmc-table')
for row in table.tbody.find_all('tr'):
# Find all data for each column
columns = row.find_all('td')
link = columns[2].a['href']
print(link)
except requests.exceptions.RequestException as e:
print(e)
