简体   繁体   中英

Pycharm can't import Beautifulsoup

OS: macOS Sierra Python ver.: installed 2.7.10 and then installed 3.5.2. (default setting 2.7.10 -> anyone know how to uninstall 2.7 or change the default setting to python 3.5 from python 2.7?) Pycharm: 2016.2.3 (Project Interpreter 3.5.2)

I have been developing a web crawler based on BeautifulSoup library but there is an error message I could never get rid of. I installed beautifulsoup4 library in Pycharm and it can even load the library, but when it meets "find" function it keeps replying error message, "NoneType' object has no attribute 'find".

I attached the captured image of an error message.

Please help me to work this out.

Thanks. Error Message

The error message mean your object title_section is null,this was the real reason to your question,not the Beautifulsoup that can't be imported.

Moreover, your web page http://52.68.130.249/textboard/ does not have a tag div which contains the class title_section , then your title_section is null which leads to the error message.

So looking at your page, the problem is that you're looking for a div with class "title_section", but the actual div is named "title-section". The underscore should be a dash.

BeautifulSoup will return None if it can't find anything.

title_section = content_table.find('div', class_='title_section')

As there is no div with a title_section class, and BeautifulSoup's find method then returns None . So therefore your call sets the Python variable title_section to None . Then when you call:

title_section.find('h1')

You're calling None.find('h1') , and the Python interpreter tells you that the NoneType object has no attribute find.

If you change the call to content_table.find('div', class_='title-section') , it will return a BeautifulSoup object, which you can call find on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM