-6

python code screenshot capture

what is the matter? about it?

html = """ <html><body>(html)(body)  
<h1>(h1)what is the scraping(/h1)</h1>
<p>(p)To analyze a web page(/p)</p> 
<p>(p)To extract the desired part(/p)</p>
(/body)(/html)</body></html> """

soup = BeautifulSoup(html, 'html.parser') 

title = soup.find(id="title")

body = soup.find(id="body")

print ("title=" + title.string)

print ("body=" + body.string)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Dj .KANG
  • 1
  • 1
  • 1
    You need to properly format the code and give us a question to answer. – Jonas Adler Jul 10 '17 at 12:09
  • Both your `soup.find` calls are returning None. Next time, you need to include the full traceback of your error. Furthermore, explain what your code is supposed to do and what you expect. – idjaw Jul 10 '17 at 12:14
  • 1
    Possible duplicate of [Python: Attribute Error - 'NoneType' object has no attribute 'something'](https://stackoverflow.com/questions/8949252/python-attribute-error-nonetype-object-has-no-attribute-something) – Alessandro Da Rugna Jul 10 '17 at 13:05
  • Hi and welcome to Stack Overflow, please take a time to go through the [welcome tour](https://stackoverflow.com/tour) to know your way around here (and also to earn your first badge), read how to create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and also check [How to Ask Good Questions](https://stackoverflow.com/help/how-to-ask) so you increase your chances to get feedback and useful answers. – DarkCygnus Jul 10 '17 at 15:40

2 Answers2

1

You are trying to find an element with id equals to title, something like <p id="title">foo bar</p>.

If you want to find tag by its type, do:

soup.find('body') # returns content of <body>

Or

soup.find('title')

The second example will not works in your case because there is no <title>foo bar</title> tag in your html, but you got the idea.

Arount
  • 9,853
  • 1
  • 30
  • 43
0

There is no title tag in your html. So, title=None. Hence you can't get any content out of it.

Try it with lxml instead of html.parser and try soup.body instead of searching for body tag.

Harshith Thota
  • 856
  • 8
  • 20