'stop process of python script?
first let me explain source: i'm writting a simple python script that search in all pages of a website and gathering special html tag with text. my code:
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)
ok, this code, work very good and display all available h1 tags with class title. result is:
[<h1 class="title>title-1</h1>"]
[<h1 class="title>title-2</h1>"]
[<h1 class="title>title-3</h1>"]
[<h1 class="title>title-4</h1>"]
but when i change code like this:
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())
the result of script just display first available (h1) tag and then script end and do not show all available tags. and result is:
title-1
what is the problem??
thanks
Solution 1:[1]
inside find_all() element having some id's has to be inside attrs={} (attribute)
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())
Solution 2:[2]
You can terminate a python process(python app) really easily.
First you have to import the python package "sys"
This package should already be installed on python and you don't have to use any pip
In order to import "sys" to your code type this line of code at the start at the code(project):
import sys
Then go to any line of code that you want to stop the python code and type in this line:
sys.exit()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nishant kumar |
| Solution 2 | AngryFox |
