Is there any modification to execute curl command in python 3.x? - python-3.x

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)

Related

ImportError: cannot import name 'HTMLSession' from 'requests_html'

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.

python mechanize import failing with python 2.x

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

python3 urllib import error

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.

AttributeError: module 'bs4' has no attribute 'BeautifulSoup'

Traceback (most recent call last):
File "bs4.py", line 1, in <module>
import bs4
File "/home/mhadi/Desktop/bs4test/bs4.py", line 5, in <module>
soup = bs4.BeautifulSoup(site,'lxml')
AttributeError: module 'bs4' has no attribute 'BeautifulSoup'
The code:
import bs4
import urllib.request
site = urllib.request.urlopen('http://127.0.0.1:8000').read()
soup = bs4.BeautifulSoup(site,'lxml')
#for i in site:
# print(site[i])
print(soup)
The problem is that your filename is bs4.py. Now if you write an import statement, Python will first look for local files with that name. So it assumes that your import bs4 refers to your own file. Your file will thus aim to import itself, but it obviously does not contain the desired module.
A quick fix is renaming the file. For instance into bs4tests.py. Then you can use import bs4.
Alternatively, you can for instance try to remove the local path, like:
import sys # import sys package
old_path = sys.path[:] # make a copy of the old paths
sys.path.pop(0) # remove the first one (usually the local)
import bs4 # import the package
sys.path = old_path # restore the import path

python 3 import not working

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.

Resources