Mechanize is not working with python 2.x while using "Browser" in mechanize failing.
import re
from mechanize import Browser
br = mechanize.Browser()
br.open("https://en.wikipedia.org/wiki/Whey_protein")
response1 = br.follow_link(text_regex=r"protein\s*shop", nr=1)
assert br.viewing_html()
print(br.title())
print(response1.geturl())
print(response1.info())
print(response1.read()) )
Traceback (most recent call last):
File "C:\Users\test\Mechnize_bt.py", line 2, in
from mechanize import Browser
ImportError: cannot import name Browser
Related
I have a runnable code in python 2.x version for following shell command :
curl -XPOST localhost:5055/parse -d '{"q":"tell me about more info on covid", "projects": "ChatBot"}'
script for python 2.x is as below :
import shutil
import sys
import urllib2
import subprocess
import json
import subprocess, sys
import os, time
import string
import os.path
import os, glob
from datetime import datetime
inputArg="tell me about more info on covid"
data = '{"q":"' + inputArg + '", "projects":"ChatBot"}'
url = 'http://localhost:5055/parse'
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
But when same script is executed in Python 3.x version:
it gives error as follows :
Traceback (most recent call last):
File "main.py", line 3, in <module>
import urllib2
ModuleNotFoundError : No module named 'urllib2'
Please let me know what changes shall be done in code so that it will execute in Python 3.x version.
What are the pre-requisites needed in python 3.x version ?
I found the solution to this issue without using urllib.request:
$ cat test6.py
import requests
import json
inputArg="tell me about more info on covid"
data = {"q":inputArg , "projects":"ChatBot"}
params = json.dumps(data)
url = 'http://localhost:5055/parse'
r = requests.post(url, data=params)
print(r.text)
print(r.status_code, r.reason)
When I tried to use the new module requests_html using the example of its website,I found the console displays information in the title.
I have successfully installed requests_html using pip install requests_html
I have updated the python to python3.7 (64-bit)
The messages of console:
Traceback (most recent call last):
File "C:/Users/owlish/PycharmProjects/python34/requests.py", line 2, in <module>
from requests_html import HTMLSession
File "C:\Users\owlish\AppData\Local\Programs\Python\Python37\lib\site-packages\requests_html.py", line 10, in <module>
import requests
File "C:\Users\owlish\PycharmProjects\python34\requests.py", line 2, in <module>
from requests_html import HTMLSession
ImportError: cannot import name 'HTMLSession' from 'requests_html' (C:\Users\owlish\AppData\Local\Programs\Python\Python37\lib\site-packages\requests_html.py)
code:
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://python.org/')
I expect it to work without an error,like the examples https://html.python-requests.org/.
With AhmedHawary's help,I found the reason for the error:I had a file named requests.py , which confilcted with the keywords . It worked fine after I renamed the file name.
I have searched some web,but did not fit to my situation. it's running fine on jupyter notebook and shell,but not on pycharm 2017.1.3.the version is Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
from urllib import request
with request.urlopen('https://api.douban.com/v2/book/2129650') as f:
data = f.read()
print('Status:', f.status, f.reason)
for k, v in f.getheaders():
print('%s: %s' % (k, v))
print('Data:', data.decode('utf-8'))
stack trace for the error
"D:\Program Files (x86)\Anaconda3\python.exe" D:/pyProject/liao/usual/urllib.py
Traceback (most recent call last):
File "D:/pyProject/liao/usual/urllib.py", line 8, in <module>
from urllib import request
File "D:\pyProject\liao\usual\urllib.py", line 8, in <module>
from urllib import request
ImportError: cannot import name 'request'
Process finished with exit code 1
I was having a similar problem. Please rename your file to something else as the import is finding your file name urllib.py before finding the urllib package.
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?
I am new to Python 3 and am rewriting a Python 2 program. I have the following file system:
|-00_programs / test.py
|-01_classes / class_scrapper.py
I want to import the class scrapper from the file class_scrapper:
Here is class_scrapper.py:
# -*- coding: utf-8 -*-
from urllib.request import urlopen
from bs4 import BeautifulSoup
class scrapper:
def get_html(self, url):
html = False
headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' }
try:
html = urlopen(url, '', headers).read()
except Exception as e:
print ("Error getting html :" + str(e))
return html
Here is test.py:
# -*- coding: utf-8 -*-
import sys
sys.path.insert(0, "./../01_classes/class_scrapper.py")
from class_scrapper import scrapper
o_scrapper = scrapper()
While executing I got:
Traceback (most recent call last):
File "/src/00_programs/tets.py", line 6, in <module>
from class_scrapper import scrapper
ImportError: No module named 'class_scrapper'
What should be changed on the import command to make that work?
Thanks,
Romain.
If the interpreter is saying module doesn't exist, it means you must have spelt it wrong when importing, or the module is either not in your program's directory or the python directory which has all of the other main modules.