I suppose this is what your expected output was:
import requests
from bs4 import BeautifulSoup
res = requests.get("https://www.fda.gov/Safety/Recalls/")
soup = BeautifulSoup(res.text, "lxml")
for item in soup.select("table td"):
if "Undeclared" in item.text:
brand = item.find_parents()[0].select("td")[1].text
reason = item.text
print(brand,reason)
Partial Output:
N/A Undeclared Milk
Colorado Nut Company and various other private labels Undeclared milk
All Natural, Weis, generic Undeclared milk
Dilettante Chocolates Undeclared almonds
Hot Pockets Undeclared egg, milk, soy, and wheat
Figiâs Undeclared Milk
Germack Undeclared Milk
When you want to get the links to the brand name as well, you can do something like below:
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
url = "https://www.fda.gov/Safety/Recalls/"
res = requests.get(url)
soup = BeautifulSoup(res.text, "lxml")
for item in soup.select("table td"):
if "Undeclared" in item.text:
brand = item.find_parents()[0].select("td")[1].text
brand_link = urljoin(url,item.find_parents()[0].select("td")[1].select("a")[0]['href'])
reason = item.text
print("Brand: {}\nBrand_link: {}\nReason: {}\n".format(brand,brand_link,reason))
Output:
Brand: N/A
Brand_link: https://www.fda.gov/Safety/Recalls/ucm587012.htm
Reason: Undeclared Milk