帮助解释代码片段

发布于 2024-09-25 15:07:17 字数 334 浏览 1 评论 0原文

我对 python 和 beautifulsoup 很陌生。

for语句中,什么是incident?它是类、类型、变量吗? for.. 后面的那行完全丢失了。

有人可以向我解释一下这段代码吗?

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

I am very new to python and beautifulsoup.

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

Can someone please 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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心不设防 2024-10-02 15:07:17

第一条语句启动一个循环,该循环解析 HTML 文档,查找宽度设置为 90% 的 td 元素。表示 td 元素的对象绑定到名称 incident

第二行是多重赋值,可以重写如下:

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

换句话说,它从 td 标记中提取内容,并为每个元素指定一个更有意义的名称。

循环中的最后一行导致循环在仅检查第一个元素后中断。该代码可以重写为不使用循环,这样可以使其更加清晰。

The first statement starts a loop which parses an HTML document looking for td elements with 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 contents 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 have been rewritten to not use a loop which would have made it more clear.

醉南桥 2024-10-02 15:07:17

欢迎来到堆栈溢出!让我们看看发生了什么。我一路上添加了进一步阅读的链接,请在提出进一步问题之前先看看它们。

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

incident只是soup返回的可迭代对象的任意局部变量。一般来说,for 语句中的局部变量 a> 可能是一个列表,但也可能是一个元组,甚至是一个字符串。如果可以迭代某些内容(例如文件),那么 Python 可能会接受 for 来遍历这些项目。

在本例中,soup 返回宽度为 90% 的 td HTML 元素列表。我们可以看到这一点,因为下一行发生的事情:

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

wherelinebreakwhat 也都是任意局部变量。它们都在一个语句中被分配。在 Python 中,这称为多重赋值。这三个元素从哪里来?incident.contents[:3] 使用 切片符号

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

这两行将 wherewhat 打印到屏幕上。 strings" rel="nofollow">strip 在做什么?它正在删除空白。因此,“一些文本” 变成了“一些文本”

        break

break 只是在第一个循环后中断 for 循环。它不会破坏整个程序。相反,它将程序流返回到循环后的下一行。

    print 'done'

这只是按照它所说的去做,将“完成”字样发送到屏幕上。如果您正在使用此程序,当您看到屏幕上出现“完成”(不带引号)时,您就知道它已完成。

1 从技术上更精确来说,它们将字节发送到标准输出(通常称为 stdout)。

Welcome to Stack Overflow! Let's take a look at what's happening. I've added links to further reading along the way, do take a look at them before asking further questions.

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

incidentis just an arbitrary local variable for the iterable returned by soup. Generally speaking, the local variable in a for statement is probably a list, but may be a tuple or even a string. If it's possible to iterate over something, like a file, then Python will probably accept for to go through the items.

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

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

where, linebreak and what are all arbitrary local variables as well. They are all being assigned in a single statement. In Python, this is known as multiple assignment. Where do those three elements come from?incident.contents[:3] is asking for the first three elements, using slice notation.

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

These two lines print where and what onto the screen.¹ But what is strip doing? It's removing white space. So, " some text " become "some text".

        break

break is just breaking the for loop after its first cycle. It doesn't break the whole program. Instead, it returns the program's flow to the next line after the loop.

    print 'done'

This is just doing what it says, sending the words 'done' to the screen. If you are using this program, you know it is complete when you see 'done' (without the quotes) appear on the screen.

¹ To be more technically precise, they send the bytes to standard out (normally known as stdout).

豆芽 2024-10-02 15:07:17

首先,Python 关心换行符和空格在哪里,因此您应该使用 code 标签来呈现 Python 代码。事实上,我必须猜测你的代码最初是如何格式化的。

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

“for x in y:”语句假定“y”是某种可迭代(类似列表)的东西 - 对象的有序集合。然后,对于列表中的每个元素,它将该元素分配给名称“x”,并运行缩进块。

在本例中,似乎有一个函数 soup(),它返回事件列表。每个事件都是一个对象,其中包含一个称为“内容”的属性,该属性本身就是一个列表; [:3] 表示“列表的前三个元素”。因此,该行获取事件内容中的前三件事,并为它们分配名称“where”、“linebreak”和“what”。 strip() 函数删除字符串开头和结尾的空格。所以我们打印“哪里”和“什么”。 'break' 从 for 循环中退出,因此在这种情况下它只运行一次,这有点奇怪。

First off, Python cares about where newlines and spaces are, so you should use the code tag to present Python code. As is, I have to guess at 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:' statement assumes that 'y' is some kind of iterable (list-like) thing - an ordered collection of objects. Then, for each element in the list, it assigns the element to the name 'x', and runs the indented block.

In this case, there appears to be a function, soup(), which returns a list of incidents. Each incident is an object which contains an attribute, called 'contents', which is itself a list; [:3] means 'the first three elements of the list'. So that line is taking the first three things in the contents of the incident and assigning them the names 'where', 'linebreak', and 'what'. The strip() function removes whitespace off the start and end of a string. So we print the 'where' and the 'what'. 'break' exits from the for-loop, so in this case it only runs once, which is a little odd.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文