urllib 与 requests 分别搭配 beautifulsoup 的不同写法

使用 urllib ,代码如下:

from urllib import request
from bs4 import BeautifulSoup 
url = 'http://www.qiushibaike.com/text/page/1'
req = request.Request(url)
req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
response = request.urlopen(req).read()
soup = BeautifulSoup(response, 'html.parser')

使用 requests ,代码如下:

import requests
from bs4 import BeautifulSoup
url = 'http://www.qiushibaike.com/text/page/1'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')





你可能感兴趣的:(python与爬虫)