def parseHtml(html): soup = BeautifulSoup(html, 'lxml') # print(soup.prettify)格式化输出 # items = soup.find_all('div', attrs={'class': 'news-list-b'}) # items = soup.select('Tag')#CSS选择器 # items = soup.select('.class')属性 # items = soup.select('#id')ID # items = soup.select('Tag[attr="属性值"]')标签+属性 # items = soup.select('.class Tag #id')空格表示子节点,组合使用 # items = soup.select('Tag').text取文本 # items = soup.select('Tag').get_text()取文本 # tag.get('className')取标签属性 # tag['className']取标签属性 # tag.attrs.get('className')取标签属性 items = soup.select('.news-list-b .list .item .title a') for item in items: yield item.get('href')#attrs字典取属性
CSS选择器 子串匹配 模糊匹配
soup = BeautifulSoup(html, 'lxml') trs = soup.select('#table_live tr[id*="tr1"]') #id包含“tr1”字符串的tr标签 #soup.select('tag[attr="属性值"]') #标签+属性
tags = soup.select('li[id^="newsLi"]') #标签+属性,id属性以newsLi开头
tags = soup.select('li[id$="newsLi"]') #标签+属性,id属性以newsLi结尾
tags = soup.select('li[id*="newsLi"]') #标签+属性,id属性包含newsLi
select带有空格的class
#div class=’content-list latest-content’ #div class=’content-list hot-content’ tags = soup.select('div.content-list.latest-content')