This article describes the Python BeautifulSoup [solution] TypeError: list indices must be integers or slices, not str, text sample code described in great detail, has a certain reference value of learning for all of us to learn or work, a friend in need the following are together with the small series to learn it
emerged in the course of python's Beautiful Soup 4 extended library
TypeError: list indices must be integers or slices, not str
This error, there is analysis of why and how to fix the error.
This error means' wrong type: list the index must be 'integers' or' slices' can not be 'str'
My error code:
#引入库
from bs4 import BeautifulSoup
#读取页面
soup = BeautifulSoup(open('index.html'))
#获取标签
img_tag = div.select("img")
#获取标签属性(这里报错)
src = img_tag['src']
#输出
print(src)
After I found the cause of the error checking comparison
It is obtained when the label is acquired rather than the tag list data
The main reasons are:
The content is acquired and they think there is bias.
That is the difference find () and find_all (), select () and select_one () of.
When
find()
select_one()
When get is a label
type
<class 'bs4.element.Tag'>
It is possible to use the tag [ 'class'] value
When
find_all()
select()
, The group obtained a label (even if there is only one label is set)
type
#find_all()的返回值类型
<class 'bs4.element.ResultSet'>
#select()的返回值类型
<class 'list'>
At this time, we have to first locate the value you need is a list (ResultSet) in the value of the tag
such as tag [0] [ 'class' ]
Solution
one:
#引入库
from bs4 import BeautifulSoup
#读取页面
soup = BeautifulSoup(open('index.html'))
#获取标签
img_tag = div.select("img")
#获取标签属性(这里有改动)
src = img_tag[0]['src']
#输出
print(src)
Because I know the structure of a page can be sure that the label on a bag to get what I needed.
Therefore, using src = img_tag [0] [ ' src'] to obtain attribute information.
Method Two:
#引入库
from bs4 import BeautifulSoup
#读取页面
soup = BeautifulSoup(open('index.html'))
#获取标签
img_tag = div.select_one("img")
#获取标签属性(这里有改动)
src = img_tag['src']
#输出
print(src)
Ibid management, such modifications are also possible success.
We recommend learning Python buckle qun: 774711191, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining, etc. [PDF, actual source code], zero-based projects to combat data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering
This problem is not because of his heart, or to remind yourself.