python 3.6爬虫案例_python3.6 简单爬虫

#coding='UTF-8'

from bs4 importBeautifulSoup # 引入beautifulsoup 解析html事半功倍importreimporturllibimporturllib.requestimportsysimportioimportjsonfrom collections importdequeimporttime

sys.stdout=io.TextIOWrapper(

sys.stdout.buffer, encoding='utf8') #改变标准输出的默认编码(这个比较重要一点,可以有效解决编码异常)defgethtml(soup):

data= soup.find_all("img")for x indata:

path= "k:/asd/" + '%s.jpg' %time.time()

fileurl= x.get("src")print(fileurl)try:

urllib.request.urlretrieve(fileurl, path)except:passurl= "http://www.toutiao.com/"queue=deque()

visited=set()

cnt=0

queue.append(url)whilequeue:

url= queue.popleft() #队首元素出队

visited |= {url} #标记为已访问

print('已经抓取:' + str(cnt) + '正在抓取

cnt+= 1

try:

urlop=urllib.request.urlopen(url)except:continue

try:

html=urlop.read().decode()except:passsoup=BeautifulSoup(html)

data=gethtml(soup)#print(data)

for x in soup.find_all('a'): # 这里提现引入beautifulsoup 的方便之处 可以直接解析html 拿到elm

#这个是beautifulsoup 文档可以看下 https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id37try:if 'http' in x.get("href") and x.get("href") not invisited:

queue.append(x.get("href"))print('加入队列 --->' + x.get("href"))except:pass

print("----------------------end-------------------")

# coding='UTF-8'from bs4 import BeautifulSoupimport reimport urllibimport urllib.requestimport sysimport ioimport jsonfrom collections import dequeimport time

sys.stdout = io.TextIOWrapper(    sys.stdout.buffer, encoding='utf8')  # 改变标准输出的默认编码

imgqu = deque()imvli = set()

def gethtml(soup):    data = soup.find_all("img")    for x in data:        path = "k:/asd/" + '%s.jpg' % time.time()        fileurl = x.get("src")        print(fileurl)        try:            urllib.request.urlretrieve(fileurl, path)        except:            pass

url = "http://www.toutiao.com/"queue = deque()visited = set()cnt = 0

queue.append(url)

while queue:    url = queue.popleft()  # 队首元素出队    visited |= {url}  # 标记为已访问

print('已经抓取: ' + str(cnt) + '   正在抓取

try:        urlop = urllib.request.urlopen(url)    except:        continue

try:        html = urlop.read().decode()    except:        pass    soup = BeautifulSoup(html)    data = gethtml(soup)    # print(data)

for x in soup.find_all('a'):        try:            if 'http' in x.get("href") and x.get("href") not in visited:                queue.append(x.get("href"))                print('加入队列 --->  ' + x.get("href"))

except:            pass

print("----------------------end-------------------")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值