python 3はbeautiful soupを利用してウェブページのテキストとsrcリンクとhttpリンクを取得します。


私は最近pythonを勉強しています。
https://github.com/Yixiaohan/show-me-the-code#rd
以下はpython 3を使ってbeautiful soupという強大な倉庫を利用して第8題を解決します。
コードは以下の通りです
from urllib import request
from bs4 import BeautifulSoup

url = 'http://sports.163.com/special/unluckykaka/'
html = request.urlopen(url)

soup = BeautifulSoup(html.read(),"html.parser")
print(soup.body.text.encode('utf-8','ignore').decode('utf-8'))
htmlの中のsrcとurlの情報を取得するには、
beautiful soupを利用すれば、この機能を実現するのに便利です。
from urllib import request
import re
from bs4 import BeautifulSoup

html = request.urlopen(r'file:///C:/Users/asus/Desktop/%E7%BD%91%E7%AB%99%E6%B5%8B%E8%AF%95.html').read()
soup = BeautifulSoup(html,"html.parser")

img_ = soup.find_all(name='img')
for each in img_:
    print(each.get('src'))
print('end')

href_ = soup.find_all(name='a')
for each in href_:
    if str(each.get('href'))[:4]=='http':
        print(each.get('href'))
print('end')