网络学习笔记
打开运行,输入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 #