python - 'ValueError:未知的 url 类型' tkinter 和 urllib
问题描述
看起来我错过了一些非常关键的东西。即使在 GUI 窗口弹出或单击按钮之前,我也会收到此错误。
当我在条目中输入数据时,它应该将其传递到“url_link”上,该“url_link”会在“get_data_url”中进一步传递。'get_data_url' 函数应该在按下按钮后执行,但它是在开始时执行的。我不确定这里有什么问题。
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'
我的代码 -
##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("\n", "")
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()
解决方案
该错误可能是由于事件驱动编程造成的,您url_here
在运行时分配了 的值,这意味着它将为空(因为该框一开始是空的),因此要修复它,请将其移至函数内部,例如:
# 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)
您可以摆脱该参数,因为您不必再使用它了。