Beautifulsoup关于find的测试

from bs4 import BeautifulSoup
import requests
url='https://book.douban.com/subject_search?search_text=golang&cat=1001'
html=requests.get(url).text
# print(html)
soup=BeautifulSoup(html,'lxml')
booknames=soup.findAll('li',{'class':'subject-item'})   #查找标签
bookname=[]
chubanshe=[]
year=[]
pingjia_price=[]
for name in booknames:
    a=name.get_text().replace(' ','').replace('
','').split('/')  #get_text方法
    # print(a)
    bookname.append(a[0])
    chubanshe.append(a[1])
    year.append(a[2])
    pingjia_price.append(a[-1])
print(bookname)
print(chubanshe)
print(year)
print(pingjia_price)

  beautifulsoup中的find和findall参数

findAll(tag,attributes,recursive,text,limit,keywords)

findAll(tag,attributes,recursive,text,keywords)

分别代表,标签,传入字典形式的标签属性,递归开关,文本匹配数量,limitpi匹配前多少项目,关键字参数

一般来说,使用,第一个和最后的关键字参数便可,其他都是默认参数,

原文地址:https://www.cnblogs.com/fengshuihuan/p/7103451.html