1. 程式人生 > >網絡爬蟲BeautifulSoup庫的使用

網絡爬蟲BeautifulSoup庫的使用

以及 a標簽 pytho 屬性 title bubuko ring iou status

使用BeautifulSoup庫提取HTML頁面信息

技術分享圖片

#!/usr/bin/python3
import requests
from bs4 import BeautifulSoup

url=http://python123.io/ws/demo.html
r=requests.get(url)
if r.status_code==200:
    print(網絡請求成功)

demo=r.text
soup=BeautifulSoup(demo,html.parser)
print(soup.prettify())

BeautifulSoup類的基本屬性

技術分享圖片

#!/usr/bin/python3
import requests
from bs4 import BeautifulSoup

url=http://python123.io/ws/demo.html
r=requests.get(url)
if r.status_code==200:
    print(網絡請求成功)

demo=r.text
soup=BeautifulSoup(demo,html.parser)
tag_title=soup.title
print(tag_title)
tag_a_attrs=soup.a.attrs
print(soup.p.
string)