Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A trouble with requests
#1
# The following will return a list of 2-grams found in the Wikipedia article on the Python programming language:

import requests
from bs4 import BeautifulSoup

def getNgrams(input, n):
	input = input.split('')
	output = []
	for i in range(len(input)-n+1):
		output.append(input[i:i+n])
	return output
	
html = requests.get("https://en.wikipedia.org/wiki/Python_(programming_language)")
bsObj = BeautifulSoup(html.content, 'html.parser')
content = bsObj.find("div", {"id":"mw-content-text"}).get_text()
ngrams = getNgrams(content, 2)
print(ngrams)
print("2-grams count is: "+str(len(ngrams))) 
Error:
Traceback (most recent call last): File "C:\Python36\kodovi\ngram.py", line 3, in <module> import requests File "C:\Python36\lib\site-packages\requests\__init__.py", line 43, in <modul > import urllib3 File "C:\Python36\lib\site-packages\urllib3\__init__.py", line 8, in <module> from .connectionpool import ( File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 11, in < odule> from .exceptions import ( File "C:\Python36\lib\site-packages\urllib3\exceptions.py", line 2, in <modul > from .packages.six.moves.http_client import ( File "C:\Python36\lib\site-packages\urllib3\packages\six.py", line 203, in lo d_module mod = mod._resolve() File "C:\Python36\lib\site-packages\urllib3\packages\six.py", line 115, in _r solve return _import_module(self.mod) File "C:\Python36\lib\site-packages\urllib3\packages\six.py", line 82, in _im ort_module __import__(name) File "C:\Python36\lib\http\client.py", line 71, in <module> import email.parser File "C:\Python36\kodovi\email.py", line 3, in <module> import smtplib File "C:\Python36\lib\smtplib.py", line 47, in <module> import email.utils ModuleNotFoundError: No module named 'email.utils'; 'email' is not a package
Any idea why these errors appear?
Reply
#2
something funky going on.
You shouldn't have any problem importing requests unless something trashed memory.
try opening an interactive python and simply importing requests
Reply
#3
Do you mean in IDLE? It doesn't give anything.
Reply
#4
I didn't mean idle, I meant to start python from command line or bash shell by typing python, but IDLE will do.
So if you don't get any message from 'IDLE' then something else is causing requests to fail on import in your new program.
If you're using an IDE, I'd exit from the IDE, reboot the OS and try again.
If it works first time after that, I would suspect that something in your code (or some other code) is tromping on memory.
Unfortunately this type of 'bug' is very hard to find as it could have been caused by something else you ran an hour ago, and only surfaced an hour later.
Reply
#5
Find a file you have called email.py and rename to eg my_email.py.
Then try Requests again.
Reply
#6
Thank you snippsat, good eye, now everything works fine.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020