一、 查找a标签
(1)查找所有a标签 (2)查找所有a标签,且属性值href中需要保护关键字“”>>> for x in soup.find_all('a',href = re.compile('lacie')): print(x)Lacie
>>> for x in soup.find_all('a',string = re.compile('Elsie')): print(x) Elsie
>>> for x in soup.find('body').children: if isinstance(x,bs4.element.Tag): #使用isinstance过滤掉空行内容 print(x)The Dormouse's story
Once upon a time there were three little sisters; and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.
>>> linklist = []>>> for x in soup.find_all('a'): link = x.get('href') if link: linklist.append(link) >>> for x in linklist: #验证:环打印出linklist列表中的链接 print(x) http://example.com/elsiehttp://example.com/laciehttp://example.com/tillie
>>> linklst = []>>> for x in soup.find_all('a', href = re.compile('elsie')): link = x.get('href') if link: linklst.append(link) >>> for x in linklst: #验证:循环打印出linklist列表中的链接 print(x) http://example.com/elsie
小结:在进行a标签查找时,加入了对属性值href内容的正则匹配内容 <---> href = re.compile('elsie')
(3)解析信息标签结构,查询所有a标签,然后输出所有标签中的“字符串”内容;>>> for x in soup.find_all('a'): string = x.get_text() print(string) ElsieLacieTillie