Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单python/漂亮的汤型问题_Python_String_Beautifulsoup - Fatal编程技术网

简单python/漂亮的汤型问题

简单python/漂亮的汤型问题,python,string,beautifulsoup,Python,String,Beautifulsoup,我正在尝试使用以下方法提取超链接的href属性执行一些简单的字符串操作: 从美化组导入美化组 汤=美汤(“”) href=soup.find(“a”)[“href”] 打印href print href[href.indexOf('/'):] 我得到的只是: Traceback (most recent call last): File "test.py", line 5, in <module> print href[href.indexOf('/'):] Attri

我正在尝试使用以下方法提取超链接的href属性执行一些简单的字符串操作:

从美化组导入美化组
汤=美汤(“”)
href=soup.find(“a”)[“href”]
打印href
print href[href.indexOf('/'):]
我得到的只是:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    print href[href.indexOf('/'):]
AttributeError: 'unicode' object has no attribute 'indexOf'
回溯(最近一次呼叫最后一次):
文件“test.py”,第5行,在
print href[href.indexOf('/'):]
AttributeError:“unicode”对象没有属性“indexOf”

如何将
href
转换为普通字符串?

Python字符串没有
indexOf
方法

使用
href.index('/')

href.find('/')
类似。但是
find
如果找不到字符串,则返回
-1
,而
index
则会引发
ValueError


因此正确的做法是使用
索引(因为“…”[-1]将返回字符串的最后一个字符)。

href是unicode字符串。如果需要常规字符串,请使用

regular_string = str(href)
你的意思是find(),而不是indexOf()


.

还值得注意的是,Unicode字符串将具有与常规字符串相同的所有方法
regular_string = str(href)