首先让我解释一下来源:我正在写一个简单的python脚本,它在网站的所有页面中搜索,并收集带有文本的特殊html标记。我的代码:
lineline = urllib.request.urlopen("http://www.test-site.com")
lineliner = lineline.read()
allsoupurl = beautifulsoup(lineliner, "html.parser")
allhtmllisturl = allsoupurl.find_all("h1", class_= "title")
print (allhtmllisturl) 好的,这段代码工作得很好,可以用类标题显示所有可用的h1标签。结果是:
[<h1 class="title>title-1</h1>"]
[<h1 class="title>title-2</h1>"]
[<h1 class="title>title-3</h1>"]
[<h1 class="title>title-4</h1>"]但是当我像这样修改代码时:
lineline = urllib.request.urlopen("http://www.test-site.com")
lineliner = lineline.read()
allsoupurl = beautifulsoup(lineliner, "html.parser")
allhtmllisturl = allsoupurl.find_all("h1", class_= "title")
for h1 in allhtmllisturl:
print (h1.get_text())脚本结果只显示第一个可用(h1)标签,然后脚本结束,并不显示所有可用标签。结果是:
title-1有什么问题吗??
谢谢
发布于 2017-04-26 14:18:58
在具有某些id的find_all()元素中,必须在attrs={} (属性)中
lineline = urllib.request.urlopen("http://www.test-site.com")
lineliner = lineline.read()
allsoupurl = beautifulsoup(lineliner, "html.parser")
allhtmllisturl = allsoupurl.find_all("h1", attrs={'class'= "title"})
for h1 in allhtmllisturl:
print (h1.get_text())发布于 2022-03-01 15:29:16
你可以很容易地终止一个python进程(Python app)。
首先,您必须导入python包"sys“。
这个包应该已经安装在python上了,不需要使用任何pip。
为了将"sys“导入到您的代码中,在代码(项目)的开头键入下面这行代码:
import sys然后转到您想要停止python代码的任何代码行,并键入此行:
sys.exit()https://stackoverflow.com/questions/43626202
复制相似问题