漂亮的汤在使用相同URL的两个不同程序中找不到相同的结果

时间:2019-01-21 20:13:32

标签: python url tkinter beautifulsoup

我创建了一个在控制台中播放的程序。我现在将其制作为GUI程序。我复制了大部分代码,但为tkinter添加了一些内容。我遇到的问题是,我认为两个程序的URL不会显示相同的信息。

import requests
import re
from bs4 import BeautifulSoup

def wholeProgram():

word = input("Enter a word: ")
webContent = requests.get('https://www.dictionary.com/browse/'+word)

soup = BeautifulSoup(webContent.text, 'html.parser')

global results
results = soup.find_all('p', attrs={'class':'one-click-content css-it69we e15kc6du7'})

print(results)

在进行故障排除时,我发现上面的代码将打印在“结果”变量中找到的内容。这证明它已成功在页面上找到了此特定内容。但是,当我对其他程序执行相同操作时,它会输出“ []”。

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


root = Tk()

askWord = Label(root, text="Enter a word")
askWord.pack()


wordEntry = Entry(root)
wordEntry.pack()
wordEntry.focus_set()

webContent = requests.get('https://www.dictionary.com/browse/'+str(wordEntry))
soup = BeautifulSoup(webContent.text, 'html.parser')

global results
results = soup.find_all('p', attrs={'class':'one-click-content css-it69we e15kc6du7'})

def callback():
   print(results)


button1 = Button(root, text="Get", width=10, command=callback)
button1.pack()

root.mainloop()

在URL的页面源中进行搜索和过滤时(例如'view-source:https://www.dictionary.com/browse/draw')。我发现我的tkinter程序中没有出现几部分内容。我知道这是因为,在非tkinter程序上,如果打印“ webContent.text”而不是“ results”,它将显示页面内容,并且您可以在内容中找到“ one-click-content css-it69we e15kc6du7” 。但是,如果您对tkinter程序执行相同操作,则找不到“一键式内容css-it69we e15kc6du7”。

1 个答案:

答案 0 :(得分:1)

单击按钮后,您必须发送请求。也可以使用get()entry框中获取值。

from tkinter import *
import requests
import re
from bs4 import BeautifulSoup
root = Tk()
askWord = Label(root, text="Enter a word")
askWord.pack()
wordEntry = Entry(root)
wordEntry.pack()
wordEntry.focus_set()

def callback():
    global results
    webContent = requests.get('https://www.dictionary.com/browse/'+str(wordEntry.get()))
    soup = BeautifulSoup(webContent.text, 'html.parser')
    results = soup.find_all('p', attrs={'class':'one-click-content css-it69we e15kc6du7'})
    print(results)

button1 = Button(root, text="Get", width=10, command=callback)
button1.pack()
root.mainloop()

“ hello”一词的输出

[<p class="one-click-content css-it69we e15kc6du7">He gets up and goes over to their table and introduces himself, and he says, ‘<span class="italic">Hello</span>, I’m Oliver Reed.</p>, <p class="one-click-content css-it69we e15kc6du7">Forty Years Young: <span class="italic">Hello</span> Kitty and the Power of Cute By Julia Rubin, Racked <span class="italic">Hello</span> Kitty is everywhere.</p>, <p class="one-click-content css-it69we e15kc6du7"><span class="italic">Hello</span> Ladies is, of course, about your British character navigating the L.A. dating scene.</p>, <p class="one-click-content css-it69we e15kc6du7">And where did the idea of the <span class="italic">Hello</span> Ladies movie come about?</p>, <p class="one-click-content css-it69we e15kc6du7">There was one incident that did happen that was dramatized in the <span class="italic">Hello</span> Ladies movie.</p>, <p class="one-click-content css-it69we e15kc6du7">Red he sees my pard passing a saloon, and he says, '<span class="italic">Hello</span>, where did you come from?</p>, <p class="one-click-content css-it69we e15kc6du7">And then, catching sight of Kirkwood's countenance: "Why, <span class="italic">hello</span>, Kirkwood!"</p>, <p class="one-click-content css-it69we e15kc6du7">"<span class="italic">Hello</span> yourself and see how you like it," the mascot of the Ravens called down.</p>, <p class="one-click-content css-it69we e15kc6du7">"<span class="italic">Hello</span>, old man," he cried, shaking Trenton warmly by the hand.</p>, <p class="one-click-content css-it69we e15kc6du7">Why couldn't he ask me how I felt or pull my ear and say "<span class="italic">Hello</span>, Puss?"</p>]