爬虫基本步骤:
1.发送请求,通过urlopen(URL),获得response
2.通过reponse.read()获得字节,如果得到像图片等二进制的字节就不需要转化了,否则通过decode()转化成字符串
3.使用正则表达式或Bs进行信息(字符模式)的提取
4.对爬取的数据进行存储
案例:名言网爬虫
from urllib.request import urlopen
import re
import csv
第一步:
url1=“http://quotes.toscrape.com/”
response=urlopen(url1)
第二步:
html_text=response.read().decode()
print(html_text)
第三步:
res_div="<div class=“quote”(.?)"
res_quote="<span class=“text” itemprop=“text”>(.?)"
res_author=“by <small class=“author” itemprop=“author”>(.?)"
res_tag="<a class=“tag” .?>(.*?)”
li=re.findall(res_div,html_text,re.S|re.M|re.I)
第四步:
with open(“c:/aa.csv”,“wt”,newline=&#