python简单爬虫
发布时间:2020-05-25 00:37:39 所属栏目:Python 来源:互联网
导读:python简单爬虫
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 import re
import urllib
import urllib.request
from collections import deque
queue = deque()#存放待爬取的网址
visited = set()#存放爬取过的网址。判断是否爬取过
url = "http://news.dbanotes.net"#入口网站
queue.append(url)
count = 1
while queue:
url = queue.popleft()#删除已经爬取过的队首的网址url
visited |= {url}#把已经爬取过的页面放入set中,方便下面的判断
urlop = urllib.request.urlopen(url)
if 'html' not in urlop.getheader('Content-Type'):
continue#如果是html再继续爬取
try:
data = urlop.read().decode('utf-8')
except:
continue
value = re.findall(r'href="(.+?)"',data)
for x in value:
if 'http' in x and x not in visited:
print("加入队列:" + x)
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
