1.获取古诗文网网页内容**
import requests
url=’http://www.gushiwen.com/‘
r=requests.get(url)
r.encoding=’UTF-8’
print(type(r))
print(r.status_code)
print(type(r.text))
print(r.text)
print(r.cookies)
G3)3PZ6_QGN3%QK__NE$HFB.png

2.下载古诗文网logo
import requests
from PIL import Image
r=requests.get(“https://www.gushiwen.com/tpl/static/images/allico.png”))
with open(‘allico.png’,’wb’)as f:
f.write(r.content)
im=Image.open(‘allico.png’)
im.show()
![}I%UG0QWKJNUHGFHOSJ8BW.png

import requests
from PIL import Image
r=requests.get(“http://www.siso.edu.cn/dfiles/11358/upload/xc.png”))
with open(‘xc.png’,’wb’)as f:
f.write(r.content)
im=Image.open(‘xc.png’)
im.show()
04NJEWAM]}S9EM~U}$V~_4X.png

3.BeautifulSoup4解析网页
from bs4 import BeautifulSoup
soup=BeautifulSoup(‘

Hello

BeautifulSoup

‘,’lxml’)
print(soup)
print(type(soup.p))
print(soup.p)
print(soup.p.name)
print(soup.p.string)
~3I8{P4TXFKS9OR_}Q$JD~1.png

from bs4 import BeautifulSoup
html=”””

  • 春天
  • “””
    soup=BeautifulSoup(html,’lxml’)
    print(soup.a.string)
    print(soup.li.string)
    print(soup.a[‘href’])
    print(soup.a[‘class’])
    print(soup.a.attrs)

    html=”””