Beautifulsoup-在soup.find()中传递变量

时间:2018-12-13 20:16:02

标签: python beautifulsoup

from tkinter import ttk
from tkinter import *
from tkinter.filedialog import askopenfilename
from PIL import ImageTk, Image

root = Tk()
root.title("python gui")
root.geometry("500x400")

def OpenFile():
    name = askopenfilename(initialdir=".",
                           filetypes =(("JPEG FILE", "*.jpg"),
                                       ("PNG FilES","*.png"),
                                       ("bitmap","*.bmp")),
                           title = "Choose a file.")
    print (name)

    try:
        pillow_img = Image.open(name)   # Open image with Pillow
        tk_img = ImageTk.PhotoImage(image=pillow_img) # Convert to PhotoImage
        original_frame.config(image=tk_img) # Put in label
        original_frame.image = tk_img       # Save reference to image
    except:
        print("Can't read file")

buttone = Button(root, text="select image", command=OpenFile)
buttone.grid(row=1,column=0)

labelframe3 = LabelFrame(root, text="original image", width=200, height=200)
labelframe3.grid(column=0, row=0, padx=20, pady=20)
labelframe3.grid_propagate(False)

original_frame = ttk.Label(labelframe3)
original_frame.grid(column=0, row=0, padx=10, pady=10)

root.mainloop()

我想通过使用变量使其动态来做与上述相同的事情。我正在使用以下代码,但是它不起作用:

result = soup.find('span', {'id': 'dlROA_ctl35_lblROALINE'}).get_text()

我收到错误消息:“ AttributeError:'NoneType'对象没有属性'get_text'”

1 个答案:

答案 0 :(得分:1)

似乎您正在尝试使用迭代器查找包含该字符串的所有跨度。您可以根据需要执行此操作,但是更好的解决方案是传递正则表达式,如下所示:

import re

results = soup.find_all('span', {'id': re.compile('dlROA_ctl\d+_lblROALINE')})
for result in results:
    print(result.get_text())

有关正则表达式的快速参考,我建议https://regex101.com

要回答您实际上提出的问题:

出现属性错误的原因不是因为代码未正确接受变量,而是因为您要获取的源代码不包含您指定的标签。

要避免出现属性错误,可以执行以下操作:

i = 35
idstring = 'dlROA_ctl'+str(i)+'_lblROALINE'
dict1 = {'id': idstring}
result = soup.find('span', dict1)
if result:
    print(result.get_text())
else:
    print('no result found')

如果您仍然找不到结果,则可能要考虑汤不是您想的那样,并且可能要看看soup.prettify()