Beautiful Soup如何提取多个class属性值?
|我使用beautifulsoup来提取class的多个属性值,但
['fa','fa-address-book-o']
不是我想要的结果。
from bs4 import BeautifulSoup
html = "<i class='fa fa-address-book-o' aria-hidden='true'></i>"
soup = BeautifulSoup(html, "lxml")
h2 = soup.select("i")
print(h2[0]['class'])
打印出结果:
['fa', 'fa-address-book-o']
这个不是我想要的结果,因为需要遍历插入mysql数据库
加入列表中的所有元素,并在它们之间放置一个空格
from bs4 import BeautifulSoup
html = "<i class='fa fa-address-book-o' aria-hidden='true'></i>"
soup = BeautifulSoup(html, "lxml")
h2 = soup.select("i")
print(' '.join(h2[0]['class']))
正确结果是:
fa fa-address-book-o
完整如下:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import pymysql
# 打开数据库连接
db = pymysql.connect(host='localhost', user='root', password='root', port=3306, db='pydemo')
# 使用cursor()方法获取操作游标
cursor = db.cursor()
url = "http://www.fontawesome.com.cn/faicons/"
# 请求网址
html = urlopen(url)
# 解析网页信息
soup = BeautifulSoup(html, "lxml")
h2 = soup.select("#new a")
for h2 in zip(h2):
name = h2[0].span.next_sibling
print(name)
icon = ' '.join(h2[0].i['class'])
print(icon)
# SQL语句
sql = 'INSERT INTO sys_icon(name,icon) values(%s,%s)'
try:
# 执行SQL语句
cursor.execute(sql, (name,icon))
# 提交到数据库执行
db.commit()
except:
# 发生错误时回滚
db.rollback()
db.close()