python - 使用 tkinter 输入一个变量,被调用

标签 python python-3.x tkinter python-3.5 tkinter-entry

我目前正在开发一种类似爬虫的程序,它将进入维基百科页面,并以其当前形式从页面中抓取引用资料。

我想要一个允许用户输入维基百科页面的图形用户界面。我希望将输入附加到 selectWikiPage 变量,但到目前为止运气不佳。

下面是我当前的代码。

import requests
from bs4 import BeautifulSoup
import re
from tkinter import *

#begin tkinter gui
def show_entry_fields():
   print("Wikipedia URL: %s" % (e1.get()))
   e1.delete(0,END)
master = Tk()
Label(master, text="Wikipedia URL").grid(row=0)
e1 = Entry(master)
e1.insert(10,"http://en.wikipedia.org/wiki/randomness")
e1.grid(row=0, column=1)
Button(master, text='Scrape', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
mainloop( )

session = requests.Session()
selectWikiPage = input(print("Please enter the Wikipedia page you wish to scrape from"))
if "wikipedia" in selectWikiPage:
    html = session.post(selectWikiPage)
    bsObj = BeautifulSoup(html.text, "html.parser")

    findReferences = bsObj.find('ol', {'class': 'references'}) #isolate refereces section of page
    href = BeautifulSoup(str(findReferences), "html.parser")
    links = [a["href"] for a in href.find_all("a", href=True)]

    for link in links:
        print("Link: " + link)

else:
    print("Error: Please enter a valid Wikipedia URL")

非常感谢。

最佳答案

这是一个基于您的代码的小型示例;它允许使用输入字段来捕获要访问的 wiki 页面的值,并将其打印在控制台上。
然后您可以使用此 URL 继续您的抓取。

from tkinter import *

def m_quit():
    global wiki_url
    wiki_url += e1.get() + '/'
    print('quitting')
    master.destroy()

wiki_url = 'http://en.wikipedia.org/wiki/'    

master = Tk()
Label(master, text="Wikipedia URL").grid(row=0)

e1 = Entry(master)
e1.grid(row=0, column=1)

Button(master, text='Scrape', command=m_quit).grid(row=3, column=0, sticky=W, pady=4)

mainloop()

print(wiki_url)

关于python - 使用 tkinter 输入一个变量,被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46464248/

相关文章:

python - 基于 Pandas Dataframe 中多列的列表值过滤索引值的最快方法?

python-3.x - 如何重复追加字符串

python-3.x - Python 3.6 - 属性错误 : module 'tkinter' has no attribute 'filedialog'

python - 比较 Pandas 中的列并合并

python - 在 python 和 tkinter 中检测碰撞

python - CESS_ESP 标签的定义

python - 使用 numpy 数组从方法创建 numpy 数组

python - 从结构不均匀的字符串中提取日期

python - Tkinter 对曲线使用什么插值?

python - 如何在 askdirectory 对话框中创建新文件夹?