mmbbflyer
#输出所得标签的‘’属性值
获取 head里面的第一个meta的content值

soup.head.meta[\'content\']
获取第一个span的内容

soup.span.string
获取第一个span的内容
soup.span.text

name属性叫keywords 所有对象
soup.find_all(attrs={\'name\':\'keywords\'})

获取样式site_name所有标签
soup.find_all(class_=\'site_name\')

soup.find(attrs={\'name\':\'keywords\'})获取第一个属性名name 值是keywords的标签
soup.find(\'meta\',attrs={\'name\':\'keywords\'})获取第一个meta标签 name属性是keywords的标签

print(soup.find(\'meta\',attrs={\'name\':\'keywords\'})[\'content\'])获取第一个meta标签name属性值是keywords的 内容值

print(soup.find(\'meta\',attrs={\'name\':re.compile(\'keywords\',re.IGNORECASE)})) 获取第一个meta标签 name属性值是 不区分大小写的keywords的标签

soup = BeautifulSoup(html,"html.parser")
meta=soup.meta//获取meta标签
attrsList = meta.attrs;//获取meta标签的所有属性元组
print "attrsList=",attrsList;
print meta.name//获取标签的名字

当html为ASCII或UTF-8编码时,可以不指定html字符编码,便可正确解析html为对应的soup:
当html为其他类型编码,比如GB2312的话,则需要指定相应的字符编码,BeautifulSoup才能正确解析出对应的soup:
htmlCharset = "GB2312";
soup = BeautifulSoup(respHtml, fromEncoding=htmlCharset);





分类:

技术点:

相关文章:

  • 2021-08-05
  • 2021-08-05
  • 2021-09-26
  • 2021-08-03
  • 2021-08-05
  • 2021-08-05
猜你喜欢
  • 2021-08-05
  • 2019-07-04
  • 2021-08-05
  • 2021-08-05
  • 2021-08-03
相关资源
相似解决方案