Python – NameError: name ‘requests’ is not defined

compiler-errorspythonpython-import

I have taken this code as for help "Python getting all links from a google search result page" .

When I try importing requests in Python 3.3.3, I get NameError: name 'requests' is not defined. I tested the "request" and "bs4" module using the CMD prompt and both show that this library has been installed.

I am trying to extract the related searched links from Google Search Result, but I don't know why I'm getting this error.

from bs4 import BeautifulSoup
page = requests.get("https://www.google.dz/search?q=see")
soup = BeautifulSoup(page.content)
import re
links = soup.findAll("a")
for link in  soup.find_all("a",href=re.compile("(?<=/url\?q=)(htt.*://.*)")):
    print (re.split(":(?=http)",link["href"].replace("/url?q=","")))

Error: Traceback (most recent call last):
  File "C:/Users/DELL/Desktop/python/s/beauti.py", line 2, in <module>
    page = requests.get("https://www.google.dz/search?q=see")
NameError: name 'requests' is not defined

Best Answer

install requests

pip install requests

and change your code like this:

from bs4 import BeautifulSoup 
import requests 
page = requests.get("https://www.google.dz/search?q=see") 
soup = BeautifulSoup(page.content) 
links = soup.findAll("a") 
for link in links: 
    if link['href'].startswith('/url?q='): 
        print (link['href'].replace('/url?q=',''))