Why do i get this "Urllib" problems? - python-3.x

ok can someone explain why this code dosen't work
import urllib.request, urllib.parse, urllib.error
fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
counts = dict()
for line in fhand:
words = line.decode().split()
for word in words:
counts[word] = counts.get(word, 0) + 1
print(counts)
i get this traceback
Traceback (most recent call last):
File "C:\Users\boris\OneDrive\Desktop\python\urllib.py", line 1, in <module>
import urllib.request, urllib.parse, urllib.error
File "C:\Users\boris\OneDrive\Desktop\python\urllib.py", line 1, in <module>
import urllib.request, urllib.parse, urllib.error
ModuleNotFoundError: No module named 'urllib.request'; 'urllib' is not a package

Related

cannot import name 'BeautifulSoup' from 'bs4'

I am using Python 3.7.0
I am not able to use BeautifulSoup attribute of bs4
I have reinstalled the library but is still not working.
from bs4 import BeautifulSoup
import csv
html = open("table.html").read()
soup = BeautifulSoup(html)
table = soup.find("table")
output_rows = []
for table_row in table.findAll('tr'):
columns = table_row.findAll('td')
output_row = []
for column in columns:
output_row.append(column.text)
output_rows.append(output_row)
with open('output.csv', 'wb') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(output_rows)
it is throwing following error:
Traceback (most recent call last):
File "D:\Automation\ReadHtml\html.py", line 1, in <module>
from bs4 import BeautifulSoup
File "D:\Python37\lib\site-packages\bs4\__init__.py", line 34, in <module>
from .builder import builder_registry, ParserRejectedMarkup
File "D:\Python37\lib\site-packages\bs4\builder\__init__.py", line 7, in <module>
from bs4.element import (
File "D:\Python37\lib\site-packages\bs4\element.py", line 19, in <module>
from bs4.formatter import (
File "D:\Python37\lib\site-packages\bs4\formatter.py", line 1, in <module>
from bs4.dammit import EntitySubstitution
File "D:\Python37\lib\site-packages\bs4\dammit.py", line 13, in <module>
from html.entities import codepoint2name
File "D:\Automation\ReadHtml\html.py", line 1, in <module>
from bs4 import BeautifulSoup
ImportError: cannot import name 'BeautifulSoup' from 'bs4' (D:\Python37\lib\site-packages\bs4\__init__.py)
Edit:
I installed using
pip install BeautifulSoup4
also installed
pip install bs4
but it is still not working.
Soved Thanks, just renamed the file from html.py to something.py

ImportError: cannot import name 'StringIO'

I am trying to get data from yahoo of stocks of a company through the code.
But i am getting an ImportError at pandas_datareader.data where is says
ImportError: cannot import name 'StringIO'
Please help
I am new to this...and already spent 4 hrs but could not resolve.
I have even tried
import io
from io import StringIO
still getting the same error..!!
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
style.use('ggplot')
start = dt.datetime(2018,1,1)
end = dt.datetime(2018,12,31)
df = web.datareader('TSLA','yahoo',start,end)
print(df.head(5))
Error:-
Traceback (most recent call last):
File "C:\Users\JAILANCHAL\Desktop\tut.py", line 5, in <module>
import pandas_datareader.data as web
File "C:\Users\JAILANCHAL\AppData\Local\Programs\Python\Python35\lib\site-packages\pandas_datareader\__init__.py", line 2, in <module>
from .data import (DataReader, Options, get_components_yahoo,
File "C:\Users\JAILANCHAL\AppData\Local\Programs\Python\Python35\lib\site-packages\pandas_datareader\data.py", line 7, in <module>
from pandas_datareader.av.forex import AVForexReader
File "C:\Users\JAILANCHAL\AppData\Local\Programs\Python\Python35\lib\site-packages\pandas_datareader\av\__init__.py", line 3, in <module>
from pandas_datareader.base import _BaseReader
File "C:\Users\JAILANCHAL\AppData\Local\Programs\Python\Python35\lib\site-packages\pandas_datareader\base.py", line 11, in <module>
from pandas.compat import StringIO, bytes_to_str
ImportError: cannot import name 'StringIO'

urllib.parse Python2.7 equivalent

What is the Python2.7 equivalent to
from urllib.parse import urlparse, parse_qs
parsed_url = urlparse(url)
params = parse_qs(parsed_url.query)
I get
>>> from urllib.parse import urlparse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named parse
Thanks!
In python2 use
from urlparse import urlparse, parse_qs
parsed_url = urlparse(url)
params = parse_qs(parsed_url.query)

ImportError: No module named url

when i run the the given code in prepare_dataset.py whose code is below as
from urllib.request
import urlopen
import json as simplejson
def main():
# Download and prepare datasets
list_generator = video_list_generator.Generator("playlists.json", youtube_api_client.Client("AIzaSyD_UC-FpXbJeWzPfscLz9RhqSjKwj33q6A"))
video_list = list_generator.get_video_list("piano")
downloader = youtube_video_downloader.Downloader()
downloader.download_from_list(video_list)
if __name__ == '__main__':
main()
as python prepare_dataset.py in command-line then i get these errors
Traceback (most recent call last):
File "prepare_dataset.py", line 1, in <module>
from urllib.request import urlopen
ImportError: No module named request
How can i get to run the above file any idea guys?

Python 3.4 : cStringIO vs. StringIO

QUESTION
I am returning an ImportError: No module named 'cStringIO'. Unfortunately cStringIO doesn't exist anymore and I need to use StringIO as a replacement. How can I do this?
import edgar
import ftplib
from io import StringIO
ftp = ftplib.FTP(edgar.FTP_ADDR)
ftp.login()
try:
edgar.download_all(ftp, "/tmp")
except Exception as e:
print(e)
finally:
ftp.close()
OUTPUT
Traceback (most recent call last):
File "/usr/local/lib/ana/lib/python3.4/site- packages/edgar/downloader.py", line 5, in <module>
from cStringIO import StringIO
ImportError: No module named 'cStringIO'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/aranjohn/PycharmProjects/edgar/secEd.py", line 1, in <module>
import edgar
File "/usr/local/lib/ana/lib/python3.4/site- packages/edgar/__init__.py", line 1, in <module>
from .downloader import FTP_ADDR, file_list, download, download_all
File "/usr/local/lib/ana/lib/python3.4/site- packages/edgar/downloader.py", line 7, in <module>
from StringIO import StringIO
ImportError: No module named 'StringIO'
Process finished with exit code 1
StringIO no longer exists in 3.x. Use either io.StringIO for text or io.BytesIO for bytes.

Resources