Help interpret the code snippet

I am very new to python and beautifulsoup.

In a for statement, what is an incident ? Is it a class, type, variable? The line following the letter .. completely lost.

Can someone explain this code to me?

 for incident in soup('td', width="90%"): where, linebreak, what = incident.contents[:3] print where.strip() print what.strip() break print 'done' 
+4
source share
3 answers

The first statement starts a loop that parses an HTML document looking for td elements with a width set to 90%. The object representing the td element is bound to the name incident .

The second line is a multiple assignment and can be rewritten as follows:

 where = incident.contents[0] linebreak = incident.contents[1] what = incident.contents[2] 

In other words, it extracts the content from the td tag and gives each element a more meaningful name.

The final line in the loop causes the loop to break after checking only the first element. The code could be rewritten so as not to use a loop that would make it more understandable.

+3
source

Welcome to stack overflow! Let's see what happens. I added links for further reading along the way, take a look at them before asking additional questions.

  for incident in soup('td', width="90%"): 

incident is just an arbitrary local variable for iterability returned by soup . Generally speaking, the local variable in for statement is probably a list, but can be a tuple or even a string. If it is possible to iterate over something, such as a file, then Python will probably accept for to view the elements.

In this case, soup returns a list of td HTML elements with a width of 90%. We can see this because of what happens on the following line:

  where, linebreak, what = incident.contents[:3] 

where , linebreak and what are also arbitrary local variables. All of them are appointed in one application. In Python, this is called multiple assignment . Where do these three elements come from? incident.contents[:3] queries the first three elements using a notation fragment .

  print where.strip() print what.strip() 

These two lines print on the where and what screen. But what does strip do? This is removing the space. So, " some text " will become "some text" .

  break 

break just breaks the for loop after the first loop. This does not violate the entire program. Instead, it returns the program stream to the next line after the loop.

  print 'done' 

It just does what it says, sending the words "done" to the screen. If you use this program, you know that it is completed when you see "done" (without quotes) on the screen.

ยน To be more technically accurate, they send bytes to the standard version (usually called stdout).

+1
source

First, Python takes care of where the lines and spaces are, so you should use a code tag to represent Python code. Like me, I have to guess how your code was originally formatted.

 for incident in soup('td', width="90%"): where, linebreak, what = incident.contents[:3] print where.strip() print what.strip() break print 'done' 

The "for x in y:" operator assumes that "y" is some iterable (list-like) thing - an ordered collection of objects. Then, for each list item, it gives the item the name "x" and launches the pending block.

In this case, the soup () function appears, which returns a list of incidents. Each incident is an object that contains an attribute called "content", which itself is a list; [: 3] means "the first three elements of the list." So this line takes the first three things in the contents of the incident and assigns them the names "where", "linebreak" and "what". The strip () function removes spaces from the beginning and end of a line. Therefore, we print "where" and "what." "break" exits the for loop, so in this case it starts only once, which is a bit strange.

0
source

All Articles