爬取的网站:http://quotes.toscrape.com/
爬取内容:名人名言
下面代码
from urllib import request
导入正则表达式包
import re req=request.Request('http://quotes.toscrape.com/') req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4549.400 QQBrowser/9.7.12900.40000') reponse=request.urlopen(req).read().decode('utf-8')
使用括号只返回括号内的内容,[]表示在一个范围内,^在[]中表示不包括,+号表示至少出现一次
req=r'<span class="text" itemprop="text">“([^"]+)”</span>'
text=re.findall(req,reponse)
用open函数创建一个txt文件,用追加模式写入
for t in text : print(t) with open('3.txt','a') as f: f.write(t+'\n')