Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Scraping Data From the Web A World Full of Spiders Everyone Loves Charlotte

Tatiane Lima
Tatiane Lima
6,738 Points

I getting this error: ImportError: No module named request

I'm trying to run this code:

from urllib.request import urlopen
from bs4 import BeautifulSoup

import re

site_links = []

def internal_links(linkURL):
    follow_html = urlopen('https://treehouse-projects.github.io/horse-land/{}'
    soup = BeautifulSoup(follow_html, 'html.parser')
    return soup.find('a', href=re.compile('(.html)$'))

def external_links(linkURL):
    follow_html = urlopen('https://{}'.format(linkURL))
    soup = BeautifulSoup(follow_html, 'html.parser')
    return soup.find('a', href=re.compile('^.(https)'))

if __name__ == '__main__':
    urls = external_links('treehouse-projects.github.io/horse-land/index.html')
    while len(urls) > 0:
        page = urls.attrs['href']
        if page not in site_links:
            urls = external_links(page)

But i'm getting this error:

Traceback (most recent call last):
  File "/.../scraping_data_from_the_web/soup_follow_scraper.py", line 1, in <module>
    from urllib.request import urlopen
ImportError: No module named request

Process finished with exit code 1

1 Answer

This works for me with python3. Both on my home PC and when I'm in workspaces. Is it possible you are running this somewhere else, and perhaps getting python2?

That's the error I get when I try to run that line with python2.

Tatiane Lima
Tatiane Lima
6,738 Points

You were right! Thanks for help me.