gpt4 book ai didi

python - Beautiful soup 没有为使用相同 URL 的两个不同程序找到相同的结果

转载 作者:行者123 更新时间:2023-12-01 08:25:18 24 4
gpt4 key购买 nike

我创建了一个在控制台中播放的程序。我现在正在把它做成一个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”而不是“结果”,它将显示页面内容,并且您可以在内容中找到“一键内容 css-it69we e15kc6du7” 。但如果您对 tkinter 程序执行相同操作,则找不到“one-click-content css-it69we e15kc6du7”。

最佳答案

您必须在单击按钮后发送请求。还可以使用 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>]

关于python - Beautiful soup 没有为使用相同 URL 的两个不同程序找到相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54297155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com