爬虫处理的对象为链接、标题、段落、图片 <a rel="nofollow" href="http:baidu.com">baidu</a> <h1>xxxx<h1> <p>xxxx<p> <img src="xxxxxxxxxx"/>


链接中有两种必须剔除的: 1、内部跳转链接 <a rel="nofollow" href="#title">xxxx</a> 2、由脚本处理的链接 <a rel="nofollow" href="javascript:void(0)">xxxx</a>


举个例子:

import requests
url="https://www.baidu.com/"
r=requests.get(url)
print(r.text)
print(r.content)
print(r.status_code)

由于爬虫需要保存大量网页,所以保存时需要保证名字不一样,常见的保存名方法有 1、domain+filename(可能重名) 2、md5(我装hashlib失败了,没法用) 3、时间戳(我的选择,时间戳的精度根据爬取的速度定,比如精确到秒还是微秒)


文件保存路径

# <a rel="nofollow"  href="http://www.baidu.com/?tn=sitehao123_15">百度</a>
url="http://www.baidu.com/?tn=sitehao123_15"
#提取domain
start_pos=url.find("//")#从前向后检索
end_pos=url.rfind('/')#从后向前检索
domain=url[start_pos+2:end_pos]
print(domain)