Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

python - 'ValueError: unknown url type in' tkinter and urllib

Looks like I am missing something very critical. I am getting this error even before the GUI window pops up or I click the button.

When I enter data in entry, it is supposed to take it and pass it onto the 'url_link' which is further passed inside 'get_data_url'. The 'get_data_url' function is supposed to be executed AFTER the button is pressed, but its being executed in the beginning. I am not sure what's wrong here.

Traceback (most recent call last):
  File "gui.py", line 100, in <module>
    btn1 = Button(win, text="Submit", command = get_data_url(url_link))
  File "gui.py", line 50, in get_data_url
    req = Request(url_link, headers={'User-Agent': 'Mozilla/5.0'})
  File "/usr/lib/python3.8/urllib/request.py", line 328, in __init__
    self.full_url = url
  File "/usr/lib/python3.8/urllib/request.py", line 354, in full_url
    self._parse()
  File "/usr/lib/python3.8/urllib/request.py", line 383, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: '/wp-json/wp/v2/posts/?per_page=100'

My code -

##GUI
import tkinter as tk
from tkinter import messagebox
from tkinter import *
win = tk.Tk()


win.geometry("300x200")
#Label
label = Label(text="URL - ")
label.place(x=20, y=50)
#Entry
entry1 = tk.Entry()
entry1.place(x=70, y=50)



#Execution

##MainCode
import os
import csv
import json
import sys
import requests
import urllib
from urllib.request import Request, urlopen, HTTPError
from urllib.parse import urlparse
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', help='To mention file')
parser.add_argument('-u', '--url', help='Passing one url')
parser.add_argument('-p', '--pages', action='store_true', help='To download pages/post')
args = parser.parse_args()


def get_urls(filename):
    urls = []

    file = open(filename, "r")

    for i in file:
        i = i.replace("
", "")
        urls.append(i)
    return urls

def get_data_url(url_link):
    req = Request(url_link, headers={'User-Agent': 'Mozilla/5.0'})
    webpage = urlopen(req).read()
    ## Fetching hostname of the URL
    parsed_uri = urlparse(url_link)
    result = '{uri.netloc}'.format(uri=parsed_uri)
    print(result)
    # Write data to file
    filename = "data/" + result + "-raw.txt"
    file_ = open(filename, 'wb')
    file_.write(webpage)
    file_.close()


    with open(filename) as json_file:
        json_data = json.load(json_file)

    C_data = []

    for n in json_data:  
    
        r={}
        r["Modified"] = n['modified']
        r["Title"] = n['title']['rendered']
        r["Content"] = n['content']['rendered']
        r["Link"] = n['link']

        # JSON Conversion

        j_data = {
            "modified/posted" : r["Modified"],
            "title" : r["Title"],
            "content" : r["Content"],
            "link" : r["Link"]
        }

        C_data.append(j_data)
        print("Title: " + r["Title"])
        print("Status: Downloaded")
        
    json_object = json.dumps(C_data, indent = 4) 

    # Writing to sample.json 
    with open("data/" + result + "-data.json", "w") as outfile: 
        outfile.write(json_object)
    print("Extracted Successfully")

urlhere = entry1.get()    
url_link = urlhere + "/wp-json/wp/v2/posts/?per_page=100"

#Button

btn1 = Button(win, text="Submit", command = get_data_url(url_link))
btn1.place(x=90, y=80)
win.mainloop()
question from:https://stackoverflow.com/questions/65933225/valueerror-unknown-url-type-in-tkinter-and-urllib

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error is probably due to event driven programming, you are assigning the value of url_here during runtime which means, it will be empty(as the box is empty at the beginning), so to fix it, move it to inside the function, like:

# Same code

def get_data_url():
    urlhere = entry1.get()    
    url_link = urlhere + "/wp-json/wp/v2/posts/?per_page=100"

    req = Request(url_link, headers={'User-Agent': 'Mozilla/5.0'})
    webpage = urlopen(req).read()
    ## Fetching hostname of the URL
    .... # Same code    

btn1 = Button(win, text="Submit", command=get_data_url)

You can get rid of the parameter as you don't have to use it anymore.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...