huy
huy

Reputation: 306

Trouble finding an html element using beautifulsoup

I have the following page- https://www.medline.com/sku/item/MDPMDS89740KDMB?skuIndex=S1&question=&flowType=browse&indexCount=1 and i want to extract the manufacturer name that is given in the table. I wrote the below code to get it but the path seems to be incorrect. I am not sure how to get only a particular td value from the table.

def name(url):
    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, 'html.parser')
    table= soup.find("table", {"class":"medSKUTableDetails"})
    mnf= table.find('td')
    if mnf:
        print(mnf.string)
    else:
        print("issue")
    

Upvotes: 1

Views: 29

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195438

You can search <td> tag with text "Manufacturer" and then find next <td> with manufacturer's name.

For example:

import requests
from bs4 import BeautifulSoup


url = 'https://www.medline.com/sku/item/MDPMDS89740KDMB?skuIndex=S1&question=&flowType=browse&indexCount=1'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

manufacturer = soup.select_one('td:contains("Manufacturer")')
if manufacturer:
    manufacturer = manufacturer.find_next('td').text
else:
    manufacturer = 'Not Found'

print(manufacturer)

Prints:

Medline

Upvotes: 3

Related Questions