Attributes

在学习写python的爬虫时,会用到BeatifulSoup来解析html。这里记录Attribute的解析方法

某个标签 <b id=''test"></b>

tag['id'] 则会输出u'test'

留下一个问题

tag=soup.tag['lang']
print(tag)
运行会得到这个结果不知道为啥,后面继续处理

哈哈这种写法是不对,没有理解到位,这样先没有找到标签的位置,是找不到attribute的值的。

for span in soup.find_all('span',class_='item_topic_title'):

  print(span.find('a').text,span.find('a')['href']) 

理解了怎么层级取到标签后。再来理解一些改变标签的操作

tag['id']='hello' #改值

tag['another-attribute']=1 #给标签里新加一个attribute 

del tag['id'] #删除attribute


原文地址:https://www.cnblogs.com/xierunfang/p/8584330.html