#๐Ÿ”’ How to find an attribute with beautiful soup library

11 messages ยท Page 1 of 1 (latest)

twilit bridge
#

So I have this code I wrote for my university's homework, and I feel like it should work, but it is not: ```py
import requests
import bs4
import sys

def find_attribute(url, html_element, attribute, output_file):
respond_object = requests.get(url)

soup_object = bs4.BeautifulSoup(respond_object.text, features="html.parser")

print(f"{url}, {html_element}, {attribute}, {output_file}")

if attribute == "final":
    writeable_file = open(output_file, 'w')
    writeable_file.write(soup_object.find(html_element)['final'])
    writeable_file.close()

else:
    desired_attribute = soup_object.find(html_element)[attribute]
    new_search_list = str(desired_attribute).split(',')
    find_attribute(str(new_search_list[0]), str(new_search_list[1]), str(new_search_list[2]), output_file)

if name == "main":
find_attribute(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])


With this command to run it:
```python3 lab21.py https://cs111.byu.edu/lab/lab21/assets/webpage1.html p mediumhunt-checkpoint1 output.txt```

With this error:

Traceback (most recent call last):
File "/home/braxtenchenay/Programming/School/lab21/lab21.py", line 25, in <module>
find_attribute(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
File "/home/braxtenchenay/Programming/School/lab21/lab21.py", line 19, in find_attribute
desired_attribute = soup_object.find(html_element)[attribute]
File "/home/braxtenchenay/.var/app/com.visualstudio.code-oss/data/python/lib/python3.10/site-packages/bs4/element.py", line 1573, in getitem
return self.attrs[key]
KeyError: 'mediumhunt-checkpoint1'

silver sorrelBOT
#

@twilit bridge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wary pewter
#

the issue with this line, ```py
desired_attribute = soup_object.find(html_element)[attribute]

#

is that attribute doesn't exist on the object so it throws an exception

#

i believe you want desired_attribute = soup_object.find(html_element).get(attribute)

#

after which you can check if desired_attribute is None. if it is, that implies the object doesn't have that attribute and you can handle it accordingly

twilit bridge
#

ahh okay, I'll try that!

#

Thanks for the help!

#

!close

silver sorrelBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.