Python - 未知的url类型错误

时间:2014-01-23 07:08:12

标签: python tkinter beautifulsoup

from tkinter import *
import tkinter as tk
import pyodbc
import urllib.request

from bs4 import BeautifulSoup,Comment

link =""

def scraper(urls):

    with urllib.request.urlopen(urls) as url:
    content = url.read()
    soup = BeautifulSoup(content, "html.parser")
    rows =soup.find_all('div',attrs={"class" : "reviewText"})
    for row in soup.find_all('div',attrs={"class" : "reviewText"}):
        print(row.text)


root1 = tk.Tk()

label1 = tk.Label(root1, text='product A')
input1 = StringVar()
entry1 = tk.Entry(root1,textvariable=input1)

label1.pack(side = tk.TOP)
entry1.pack()


buttonstr = tk.StringVar()
db = r"C:\Users\Goutham\Documents\keshav\testdb.accdb"


print("connecting db..")

def odbc():
     '''
 `enter code here`connects with odbc
     '''        
  global link
  constr = 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=' + db
  conn = pyodbc.connect(constr, autocommit=True)
  cur = conn.cursor()
  check=input1.get()
  print("fetching from access.....")
  strsql = "select Url from student where PdtName='%s' " % (check,)
  cur.execute(strsql)
  results = cur.fetchall()
  link=check
  print (results,check)
  conn.close()


buttonA = tk.Button(text = "hello", command = odbc)

buttonA.pack()



scraper(link)

我需要此代码来获取输入,将其存储在变量-'check中,并使用SQL查询将其与数据库中的值进行比较。数据库中的匹配值用于从数据库中检索URL。 URL作为参数传递给函数scraper(),用于打印提取的文本。

显示以下错误:

Traceback (most recent call last):
  File "C:\Python33\module1.py", line 62, in <module>
    scraper(link)
  File "C:\Python33\module1.py", line 13, in scraper
    with urllib.request.urlopen(urls) as url:
  File "C:\Python33\lib\urllib\request.py", line 156, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python33\lib\urllib\request.py", line 454, in open
    req = Request(fullurl, data)
  File "C:\Python33\lib\urllib\request.py", line 275, in __init__
    self._parse()
  File "C:\Python33\lib\urllib\request.py", line 280, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: ''

请帮忙。

谢谢。

1 个答案:

答案 0 :(得分:3)

您在脚本末尾调用scraper(link),在那一刻link是空字符串。这就是你得到ValueError: unknown url type: ''的原因。

删除该语句并在odbc回调函数中对URL格式进行验证。