I have this code that loads the natural gas storage numbers from the internet.
from urllib.request import urlopen
print(int(str(urlopen("http://ir.eia.gov/ngs/wngsr.txt").read()).split("\\n")[4].split(" ")[2]))
How could I do this in one line? More specifically, I was wondering how I could get rid of the import line and do something like this:
print(int(str(urllib.request.urlopen("http://ir.eia.gov/ngs/wngsr.txt").read()).split("\\n")[4].split(" ")[2]))
(I changed the urlopen call to urllib.request.urlopen. It would be sort of like Java, if you use the fully qualified name, you don't need an import statement.)
When trying out the suggestion by Zizouz212, I found this works
print(int(str(__import__('urllib').request.urlopen("http://ir.eia.gov/ngs/wngsr.txt").read()).split("\\n")[4].split(" ")[2]))
You always need the import, however you can still use semi-colons to separate statements:
from urllib.request import urlopen; print(int(str(urllib.request.urlopen("http://ir.eia.gov/ngs/wngsr.txt").read()).split("\\n")[4].split(" ")[2]))
# note the semi-colon ^
Related
I know how to import functions from normal files...
but my file name is 'selection sort'
it contains a space in between
is there a way to import function from these without renaming it
I tried import selection sort and import selection_sort… both didn't work
I also watched few videos and questions like these but can't find the solution.
You can use the __import__() function like:
selection_sort = __import__("selection sort")
and then the file selection sort is imported as selection_sort.
While it is not recommended to have a space in the file name, you could try:
selection_sort = __import__("selection sort")
Im new to python and wondering if there is a way for it to open a webpage depending on whats been inputted. EG
Market=input("market")
ticker=input("Ticket")
would take you to this part of the website.
https://www.tradingview.com/symbols/'market'-'ticker'/technicals
Thanks
Looks like you were pretty much there, but it python you can use the + sign to concatenate strings and then cause it to open that link using webbrowser library
import webbrowser
market=input("market")
ticker=input("Ticket")
webbrowser.open('https://www.tradingview.com/symbols/'+market+'-'+ticker+'/technicals')
Its cleaner to use format string like this:
import webbrowser
market=input("market")
ticker=input("Ticket")
webbrowser.open(f'https://www.tradingview.com/symbols/{market}-{ticker}/technicals')
I have two Python files. In my main file I work with a openpyxl module. In my second file I have many string lines with concatenating using Excel file cells, for example:
'/ip address=' + sheet['D'+ row].value + '\n'
and many others. But there is a problem, if I import that file to a main file using:
from file2 import *
I get many errors about undefined names like:
NameError: name 'sheet' is not defined
And it is really defined only in my main file, like:
wb = openpyxl.load_workbook(filename='clients.xlsx')
sheet = wb.get_sheet_by_name('Page1')
How can I import everything from my file2 and get it work?
As far as I can wrap my head around it, import only imports functions. execfile(*path*) should work for you in your case.
There are some more ways to import python into python, which you might want to check out.
I'm trying to get into CTF's and I found a cool website ment to practice some web based CTF skills called ctf.slothparadise.com. I've managed to get 4 of the Flags but two of them are giving me the finger and sadly I've had to dust off the good Ol' Python skills.
import urllib.error
import urllib.request
import urllib.parse
import urllib
import sys
while True:
about_page = urllib.request.urlopen("http://ctf.slothparadise.com/about.php").read()
if "KEY" in about_page:
print(about_page)
sys.exit(0)
ctf.slothpython.com/about.php is the page I'm programming for and it spits out the key in the source code every 1000 visitors. Instead of being a moron and refreshing it till 1000 I wrote that code in hopes it would keep opening the page until the phrase "KEY" appeared in the pages source code.
I'm getting this: (TypeError: 'str' does not support the buffer interface)
From what I know about TypeErrors I'm guessing that I may have "KEY" in the wrong format perhaps? I'm not really sure, I also may not even be using the right modules but the old urllib2 module I would typically use for this got split up into different modules so I'm learning as I go with these new modules.
Any help is appreciated in fixing this issue, also if my interpretaion of TypeErrors is wrong feel free to correct me.
The object returned by urlopen().read() acts like a context manager.
You are not using it correctly.
Try something like that:
import urllib.request
while True:
with urllib.request.urlopen('http://ctf.slothparadise.com/about.php') as response:
html = response.read()
if b"KEY" in html:
print(html)
sys.exit(0)
urllib.request.urlopen returns an http.client.HTTPResponse object and that object's read returns an encoded bytes object. How to decode may be in the returned http header, or in your case, embedded in an html meta tag. You likely don't want to parse the html for this particular test, so just look for the bytes object b'KEY'.
I don't know what you want to do with the data next, but if you want it to print nicely or scan the html, then you will have to do some parsing.
import urllib.error
import urllib.request
import urllib.parse
import urllib
import sys
while True:
about_page = urllib.request.urlopen("http://ctf.slothparadise.com/about.php").read()
if b"KEY" in about_page:
print(about_page)
sys.exit(0)
Make about_page a string with
about_page=str(urllib.request.urlopen("http://ctf.slothparadise.com/about.php").read())
This should make your code work. Hope this helps!!
I tried to download something from the Internet using Python, I am using urllib.retriever from the urllib module but I just can't get it work. I would like to be able to save the downloaded file to a location of my choice.
If someone could explain to me how to do it with clear examples, that would be VERY appreciated.
I suggest using urllib2 like so:
source = urllib2.urlopen("http://someUrl.com/somePage.html").read()
open("/path/to/someFile", "wb").write(source)
You could even shorten it to (although, you wouldnt want to shorten it if you plan to enclose each individual call in a try - except):
open("/path/to/someFile", "wb").write(urllib2.urlopen("http://someUrl.com/somePage.html").read())
You can also use the urllib:
source = urllib.request.urlopen(("full_url")).read()
and then use what chown used above:
open("/path/to/someFile", "wb").write(source)