Nurdin
Nurdin

Reputation: 23883

How to retrieve href that contain specific text in Beautifulsoup 4?

My soup

import requests
from bs4 import BeautifulSoup

page = requests.get('https://example.com')
soup = BeautifulSoup(page.text, 'html.parser')

property_list = soup.find(class_='listing-list ListingsListstyle__ListingsListContainer-cNRhPr hqtMPr')
property_link_list = property_list.find_all('a',{ "class" : "depth-listing-card-link" },string="View details")
print(property_link_list)

I just got an empty array. What I need is to retrieve all the hrefs that contain View details text.

This is an example of the input

<a class="depth-listing-card-link" href="https://example.com">View details<i class="rui-icon rui-icon-arrow-right-small  Icon-cFRQJw cqwgEb"></i></a>

I am using Python 3.7.

Upvotes: 1

Views: 235

Answers (1)

Jack Fleeting
Jack Fleeting

Reputation: 24930

Try changing the last 2 lines of your code to:

property_link_list = property_list.find_all('a',{ "class" : "depth-listing-card-link" })
for pty in property_link_list:
    if pty.text=="View details":
        print(pty['href'])

My output is:

/property/bandar-sungai-long/sale-7700845/
/property/bandar-sungai-long/sale-7700845/
/property/bandar-sungai-long/sale-4577620/
/property/bandar-sungai-long/sale-4577620/
/property/port-dickson/sale-8387235/
/property/port-dickson/sale-8387235/

etc.

Upvotes: 1

Related Questions