0
from gettext import find
import mechanize
browser = mechanize.Browser()
browser.set_handle_robots(False)
cookies = mechanize.CookieJar()
browser.set_cookiejar(cookies)
browser.addheaders = [('user_agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36')]
browser.set_handle_refresh(False)
from bs4 import BeautifulSoup

url = "https://www.example.com/"
browser.open(url)
browser.select_form(nr = 0)     
browser.form['email'] = '12345@gmail.com'
browser.form['password'] = 'sfefgdg'
response = browser.submit()
print (response.read()) 

how can I get a contain part of an html element by id or name- python - browser or beautifulsoup.

When I use print(response.read()) I was able to print out the HTML, but with b'<html> <html>' binding the html element, so how can I print without getting that b'' banding.

when I use print(response) I got b'' only without the html. what I wanted is to get an element by id or name in the html output from the url, but I think with the b'' binding the html I could not get to the html element.

#output:
b'<?xml version="1.0" encoding="utf-8"?>\n<
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    <HTML>
       <HEAD>
          <TITLE>My first HTML document</TITLE>
       </HEAD>
       <BODY>
          <P>Hello world!
           <DIV class="subsection" id="forest-habitat" >
       </BODY>
    </HTML>'

I wanted to use beautifulsoup to get a particular element by id or name , but all I can get is:

soup = BeautifulSoup(response, 'html.parser')
    result = soup.find('div', {'id':'forest-habitat'})
    print(result)

output: none

so how can I get the element by id or name?

DeziLuv
  • 69
  • 6

0 Answers0