brendosthoughts brendosthoughts - 1 year ago 62
Python Question

Beautiful soup not picking u certain tags

Okay, I have read a lot of the answers on here about working with beautiful soup. and still am not having luck getting what I need done here is my

soup = BeautifulSoup(open("/home/brendan/PycharmProjects/untitled2/newDeficency.html"),"html5lib")

for element in soup.find_all('input'):
print(element['name'], element['value'])


Since this is part of a larger program for now I just cached a copy of the page type i'd like to scrape now this currently returns nothing but the following code returns all the hyperlinks in the document

for element in soup.find_all('a'):
print(element['href'])


I am unsure why this is not working properly as i have also tried using

'li' , 'select' 'option' and 'form'


with no success despite them clearly being in the source here is a here is a link to source page

be greatly appreciated as I've been messing with this 6 lines of code for a few hours now and am completely lost

Answer Source

This might be what you need.

HTML = '''\
<html>
<body>
<form>
    <input type='text' name='name' value='jones'></input>
    <input type='text' name='rank' value='private'></input>
    <input type='text' name='serial_number' value='B18567'></input>
</form>
</body>
</html>'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(HTML, 'lxml')
for element in soup.find_all('input'):
    print (element.attrs['name'], element.attrs['value'], element.attrs['type']) 

Output:

name jones text
rank private text
serial_number B18567 text

EDIT: You should see something like this in the browser window when you open the file you've downloaded in the browser.

upper left corner of browser window

Now, in Windows Ctrl-A, Ctrl-C and put clipboard contents into a file called temp.htm.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download