網絡爬蟲-課時5 用BeautifulSoup 剖析網頁元素

網絡學習筆記


打開運行,輸入jupyter book

new(新建) python3

import requests
res = requests.get('http://news.sina.com.cn/')
res.encoding = 'utf-8'
#print (res.text)

from bs4 import BeautifulSoup
html_sample = ' \
<html> \
 <body> \
 <h1 id="title">Hello World</h1> \
 <a href="#" class="link">This is link1</a> \
 <a href="# link2" class="link">This is link2</a> \
 </body> \
 </html>'
soup = BeautifulSoup(html_sample, 'html.parser')
print(soup.text)
Hello World This is link1 This is link2 

soup = BeautifulSoup(html_sample, 'html.parser')
header = soup.select('h1')

print(header[0].text)
Hello World

soup = BeautifulSoup(html_sample, 'html.parser')
alink = soup.select('a')
for link in alink:
    print(link['href'])
#
# link2
a = '<a href = "#" qoo=123 abc=456> i am a link<a>'
soup2 = BeautifulSoup(a, 'html.parser')
print(soup2.select('a')[0]['qoo'])

print(soup2.select('a')[0]['href'])
123
#

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章