# coding='UTF-8'
from bs4 import BeautifulSoup # 引入beautifulsoup 解析html事半功倍
import re
import urllib
import urllib.request
import sys
import io
import json
from collections import deque
import time sys.stdout = io.TextIOWrapper(
sys.stdout.buffer, encoding='utf8') # 改变标准输出的默认编码(这个比较重要一点,可以有效解决编码异常)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) + ' 正在抓取 <--- ' + url)
cnt += 1 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'): # 这里提现引入beautifulsoup 的方便之处 可以直接解析html 拿到elm
                    #这个是beautifulsoup 文档可以看下 https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id37
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-------------------")

# 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) + '   正在抓取 <---  ' + url)    cnt += 1
    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-------------------")

最新文章

  1. gitlab两种连接方式:ssh和http配置介绍
  2. hdu 2177(威佐夫博奕)
  3. HDU 4609 3-idiots (FFT-快速傅立叶变换)
  4. PHP学习笔记 - 进阶篇(7)
  5. Python脚本控制的WebDriver 常用操作 &lt;二十四&gt; 定位frame中的元素
  6. 网络防火墙实战-基于pfsense(1)
  7. for循环:用turtle画一颗五角星
  8. 研发团队如何写好API接口文档
  9. IDE中使用System.getProperty()获取一些属性
  10. Cglib动态代理实现方式
  11. Uva297 Quadtrees【递归建四分树】【例题6-11】
  12. ThinkPHP5分页样式设置
  13. (转)Unity3D研究院之手游开发中所有特殊的文件夹(assetbundle与Application.persistentDataPath)
  14. SCSS 实用知识汇总
  15. maven子模块转化成project
  16. js获取鼠标坐标位置兼容多个浏览器
  17. HDU 4553 约会安排(线段树区间合并+双重标记)
  18. 线性回归 Linear regression(4) 局部加权回归
  19. hdu1350Taxi Cab Scheme (最小路径覆盖)
  20. Row_Number() OVER()函数使用举例

热门文章

  1. 浅谈Django的Q查询以及AngularJS的Datatables分页插件
  2. 拆开Ceph看队列和线程
  3. shiro的入门实例-shiro于spring的整合
  4. c#入门系列——基础篇
  5. DFS 分布式文件系统 选型笔记
  6. mac 终端简单指令
  7. weex官方demo weex-hackernews代码解读(1)
  8. 微信小程序 网络请求之设置合法域名
  9. linux下常用语言的语法检查插件整理
  10. java生成二维码