Findall 用于识别已下载 HTML 页面中的特定标题
Findall to identify a specific title in a downloaded HTML page
我必须下载 HTML 页面的源代码,然后必须找到特定的标题并将它们打印在 Python 的 GUI 上。我可以下载 HTML 文件,但无法在下载的 HTML 页面中找到我感兴趣的元素。举个例子,我目前正在使用这个网页。
https://www.metacritic.com/browse/games/release-date/coming-soon/all/date
第一个发布的游戏是"Lovecraft's Untold Stories"。我想要这个标题并在我的 GUI 中打印。在HTML页面中,这个标题是通过
标签打印出来的。我正在使用 Findall 方法,但它 returns 什么都没有。
P.S。我不能使用任何其他库,包括 Beautiful Soap 或 requests。我只能使用 urllib、findall、finditer、MULTILINE、DOTALL。目前实现的一段代码如下所示。
def game_function():
game = Tk()
game.geometry('600x400')
game.title('Upcoming Video Game Releases')
game.resizable(0,0)
opener = urllib.request.FancyURLopener({})
url = "file:///D:/Riz/Poppie%202/Upcoming%20Video%20Game%20Releases%20for%202019%20-%20Metacritic.html"
with urllib.request.urlopen(url) as response:
encoding = response.info().get_param('charset', 'utf8')
html = response.read().decode(encoding)
print("",html)
title_tag = '<h3>(.*)</h3>'
title_1 = findall(title_tag, html)
print("",title_1)
title1_subtitle = Label(game, text = title_1, bg='white', fg='black', font = ('Arial', 14, 'bold'))
title1_subtitle.place(relx=0.8, rely=0.49)
title1_subtitle.configure(wraplength='260')
game.mainloop()
问题是由于 h3
标签内有换行符 (\n
)。在 re.findall
的第一个参数中,.
表示 除换行符 之外的任何字符,除非您使用 re.DOTALL
作为第三个参数。此外,您应该使用 non-greedy 版本。
我希望下面的例子能说明问题:
import re
txt = '''<h3>
SomeTitle
</h3>
AnotherContent
<h3>
AnotherTitle
</h3>'''
nodotall = re.findall('<h3>(.*)</h3>',txt)
withdotall = re.findall('<h3>(.*)</h3>',txt,re.DOTALL)
nongreedy = re.findall('<h3>(.*?)</h3>',txt,re.DOTALL)
print(nodotall) # [] i.e. nothing found
print(withdotall) # ['\nSomeTitle\n</h3>\nAnotherContent\n<h3>\nAnotherTitle\n'] i.e. everything between first <h3> and last </h3>
print(nongreedy) # ['\nSomeTitle\n', '\nAnotherTitle\n'] i.e. desired output
我必须下载 HTML 页面的源代码,然后必须找到特定的标题并将它们打印在 Python 的 GUI 上。我可以下载 HTML 文件,但无法在下载的 HTML 页面中找到我感兴趣的元素。举个例子,我目前正在使用这个网页。
https://www.metacritic.com/browse/games/release-date/coming-soon/all/date
第一个发布的游戏是"Lovecraft's Untold Stories"。我想要这个标题并在我的 GUI 中打印。在HTML页面中,这个标题是通过
标签打印出来的。我正在使用 Findall 方法,但它 returns 什么都没有。 P.S。我不能使用任何其他库,包括 Beautiful Soap 或 requests。我只能使用 urllib、findall、finditer、MULTILINE、DOTALL。目前实现的一段代码如下所示。def game_function():
game = Tk()
game.geometry('600x400')
game.title('Upcoming Video Game Releases')
game.resizable(0,0)
opener = urllib.request.FancyURLopener({})
url = "file:///D:/Riz/Poppie%202/Upcoming%20Video%20Game%20Releases%20for%202019%20-%20Metacritic.html"
with urllib.request.urlopen(url) as response:
encoding = response.info().get_param('charset', 'utf8')
html = response.read().decode(encoding)
print("",html)
title_tag = '<h3>(.*)</h3>'
title_1 = findall(title_tag, html)
print("",title_1)
title1_subtitle = Label(game, text = title_1, bg='white', fg='black', font = ('Arial', 14, 'bold'))
title1_subtitle.place(relx=0.8, rely=0.49)
title1_subtitle.configure(wraplength='260')
game.mainloop()
问题是由于 h3
标签内有换行符 (\n
)。在 re.findall
的第一个参数中,.
表示 除换行符 之外的任何字符,除非您使用 re.DOTALL
作为第三个参数。此外,您应该使用 non-greedy 版本。
我希望下面的例子能说明问题:
import re
txt = '''<h3>
SomeTitle
</h3>
AnotherContent
<h3>
AnotherTitle
</h3>'''
nodotall = re.findall('<h3>(.*)</h3>',txt)
withdotall = re.findall('<h3>(.*)</h3>',txt,re.DOTALL)
nongreedy = re.findall('<h3>(.*?)</h3>',txt,re.DOTALL)
print(nodotall) # [] i.e. nothing found
print(withdotall) # ['\nSomeTitle\n</h3>\nAnotherContent\n<h3>\nAnotherTitle\n'] i.e. everything between first <h3> and last </h3>
print(nongreedy) # ['\nSomeTitle\n', '\nAnotherTitle\n'] i.e. desired output