使用:
import re
for tag in soup.find_all(re.compile("^b")):
print(tag.name)
# body
# b
#取得a標籤裡開頭為http://www.aaa.com/的超連結,的文字
_tag = soup.find('a',attrs={'href':re.compile(r'http://www.aaa.com/(.*)')}).get_text()
1、title
1 ------------
# 爬取HTML中的title
res = re.findall(r"<title>(.+?)</title>", html)
print(" Page title is: ", res[0])
2 ------------
#輸出標題
print(soup.title)
------------
soup.select("a[href]") --》 选择带有href属性的<a> tag.
------------
soup.select('div[title*="关键字"]') --》选择 title属性含有 “关键字“ 的<div> tag.
------------
#正则表达式re.compile()
------------
# 爬取段落
res = re.findall(r"<p>(.*?)</p>", html, flags=re.DOTALL) # re.DOTALL if multi line
print(" Page paragraph is: ", res[0])
# 爬取网页中所有超链接 href
res = re.findall(r'href="(.*?)"', html)
print(" All links: ", res)