'ValueError: unknown url type in' tkinter and urllib
'ValueError: unknown url type in' tkinter and urllib
看来我遗漏了一些非常重要的东西。甚至在 GUI window 弹出或我单击按钮之前我就收到此错误。
当我在条目中输入数据时,它应该将其传递到 '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()
你应该在 提交 按钮的回调中获取 Entry
的内容,构造 URL 并调用 get_data_url()
:
def submit():
urlhere = entry1.get()
url_link = urlhere + "/wp-json/wp/v2/posts/?per_page=100"
get_data_url(url_link)
btn1 = Button(win, text="Submit", command=submit)
错误可能是由于事件驱动编程引起的,您在运行时分配了 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)
您可以删除该参数,因为您不必再使用它了。
看来我遗漏了一些非常重要的东西。甚至在 GUI window 弹出或我单击按钮之前我就收到此错误。
当我在条目中输入数据时,它应该将其传递到 '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()
你应该在 提交 按钮的回调中获取 Entry
的内容,构造 URL 并调用 get_data_url()
:
def submit():
urlhere = entry1.get()
url_link = urlhere + "/wp-json/wp/v2/posts/?per_page=100"
get_data_url(url_link)
btn1 = Button(win, text="Submit", command=submit)
错误可能是由于事件驱动编程引起的,您在运行时分配了 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)
您可以删除该参数,因为您不必再使用它了。