您的位置:首页 > 编程语言 > Python开发

Python常见问题和技巧

2015-11-30 17:34 645 查看

Python常见问题和技巧

参数的class和class_使用

在某些函数的参数中,可能需要用到有名参数class,因为class又是语言本身的关键字,所以使用class_。

例如:

from bs4 import BeautifulSoup
...


# 指定class查找html源码
>>> for t in soup.find_all(class="sister"):
print(t)

SyntaxError: invalid syntax


应换成:

>>> for t in soup.find_all(class_="sister"):
print(t)

<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: