Jus*_*tin 4 python string beautifulsoup
我正在尝试使用Beautiful Soup提取的超链接的href属性进行一些简单的字符串操作:
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<a href="http://www.some-site.com/">Some Hyperlink</a>')
href = soup.find("a")["href"]
print href
print href[href.indexOf('/'):]
Run Code Online (Sandbox Code Playgroud)
我得到的只是:
Traceback (most recent call last):
File "test.py", line 5, in <module>
print href[href.indexOf('/'):]
AttributeError: 'unicode' object has no attribute 'indexOf'
Run Code Online (Sandbox Code Playgroud)
我应该如何将href正常的字符串转换成什么?
Python字符串没有indexOf方法.
使用 href.index('/')
href.find('/')类似.但是如果找不到字符串则find返回-1,同时index引发a ValueError.
所以正确的方法是使用index(因为'...'[ - 1]将返回字符串的最后一个字符).
| 归档时间: |
|
| 查看次数: |
3724 次 |
| 最近记录: |