1. # 获取百度网站首页上的所有a标签里的 href属性值:
  2. # import requests
  3. # from bs4 import BeautifulSoup
  4. # # html = requests.get('http://en.wikipedia.org/wiki/Kevin_Bacon')
  5. # html = requests.get('http://www.baidu.com')
  6. # bs = BeautifulSoup(html.text, 'html.parser')
  7. # for link in bs.find_all(lambda tag: 'href' in tag.attrs):
  8. # print(link.attrs['href'])
  9. # import requests
  10. # import re
  11. # from bs4 import BeautifulSoup
  12. # # html = requests.get('http://en.wikipedia.org/wiki/Kevin_Bacon')
  13. # html = requests.get('http://www.baidu.com')
  14. # bs = BeautifulSoup(html.text, 'html.parser')
  15. # for link in bs.find_all('', {'href':re.compile('\.com')}):
  16. # print(link.attrs['href'])
  17. # import requests
  18. # from bs4 import BeautifulSoup
  19. # html = requests.get('http://www.baidu.com')
  20. # bs = BeautifulSoup(html.text, 'html.parser')
  21. # for link in bs.find_all('a'):
  22. # if 'href' in link.attrs:
  23. # print(link.attrs['href'])
  24. import requests
  25. from bs4 import BeautifulSoup
  26. def geturl(url):
  27. html = requests.get(url)
  28. bs = BeautifulSoup(html.text, 'html.parser')
  29. return bs.find_all('a')
  30. links=geturl('http://www.baidu.com')
  31. for link in links:
  32. if 'href' in link.attrs:
  33. print(link.attrs['href'])
  1. http://news.baidu.com
  2. http://www.hao123.com
  3. http://map.baidu.com
  4. http://v.baidu.com
  5. http://tieba.baidu.com
  6. http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1
  7. //www.baidu.com/more/
  8. http://home.baidu.com
  9. http://ir.baidu.com
  10. http://www.baidu.com/duty/
  11. http://jianyi.baidu.com/

  1. import requests
  2. from bs4 import BeautifulSoup
  3. def geturl(url):
  4. html = requests.get(url)
  5. bs = BeautifulSoup(html.text, 'html.parser')
  6. return bs.find_all('', {'href':re.compile('http://')})
  7. # links=geturl('http://www.baidu.com')
  8. # print(list)
  9. # links_in_news=set(geturl(links[1].attrs['href'])) # set(list) 去重变为集合
  10. # for link in links_in_news:
  11. # print(link.attrs['href'])
  12. print('-------------------------------------------------------------------------')
  13. links=geturl('http://www.baidu.com')
  14. for link in links:
  15. if '//news.' in link.attrs['href']:
  16. links_in_news=set(geturl(link.attrs['href'])) # set(list) 去重变为集合
  17. for link in links_in_news:
  18. print(link.attrs['href'])
  19. break
  1. -------------------------------------------------------------------------
  2. http://baijiahao.baidu.com/s?id=1670182176542294758
  3. http://baijiahao.baidu.com/s?id=1670237336710694101
  4. http://baijiahao.baidu.com/s?id=1670287125142703268
  5. http://baijiahao.baidu.com/s?id=1670255408896313915
  6. http://baijiahao.baidu.com/s?id=1670191066909619203
  7. http://v.baidu.com/
  8. http://baijiahao.baidu.com/s?id=1670253988609166598
  9. http://news.cctv.com/2020/06/23/ARTIHsG0yhCaD2YJUSFy7Qwt200623.shtml
  10. http://baijiahao.baidu.com/s?id=1670286769270600802
  11. http://news.cctv.com/2020/06/23/ARTIpnapIHyb413WeY46ShDy200623.shtml
  12. http://m.top.cnr.cn/bdxw/20200623/t20200623_525141426.html
  13. http://world.people.com.cn/n1/2020/0623/c1002-31756267.html
  14. http://m.news.cctv.com/2020/06/23/ARTIDAQdwzQFMOkbW2Z0ehEk200623.shtml
  15. http://baijiahao.baidu.com/s?id=1670245143050480742
  16. http://m.news.cctv.com/2020/06/18/ARTIYNwiYAjjHBmGeAXpERs3200618.shtml
  17. http://m.xinhuanet.com/yn/2020-06/23/c_139161263.htm
  18. http://baijiahao.baidu.com/s?id=1670194818426496533
  19. http://baijiahao.baidu.com/s?id=1670232858345398185
  20. http://www.xinhuanet.com/2020-06/23/c_1126147531.htm
  21. http://baijiahao.baidu.com/s?id=1670251112933488182
  22. http://baijiahao.baidu.com/s?id=1670254276238905964
  23. http://baijiahao.baidu.com/s?id=1670255017218969710
  24. http://music.baidu.com/
  25. http://m.top.cnr.cn/bdxw/20200623/t20200623_525141422.html
  26. http://app.cctv.com/special/cportal/detail/arti/index.html?id=Arti8bFV6wkTJPYEkaZYVvoC200622&fromapp=cctvnews&version=805&allow_comment=1&allow_comment=1
  27. http://map.baidu.com/
  28. http://baijiahao.baidu.com/s?id=1670243226621040644
  29. http://baijiahao.baidu.com/s?id=1670254944449236682
  30. http://net.china.cn/chinese/index.htm
  31. http://baijiahao.baidu.com/s?id=1670250874637091231
  32. http://baijiahao.baidu.com/s?id=1670232858345398185
  33. http://baijiahao.baidu.com/s?id=1670289098569528699
  34. http://baijiahao.baidu.com/s?id=1670247580845339645
  35. http://baijiahao.baidu.com/s?id=1670254849012760202
  36. http://m.top.cnr.cn/bdxw/20200623/t20200623_525141424.html
  37. http://baijiahao.baidu.com/s?id=1670246144336669257
  38. http://baijiahao.baidu.com/s?id=1670254276238905964
  39. http://app.cctv.com/special/cportal/detail/arti/index.html?id=ArtiLXGGutc9OLD23xo3Y3dN200622&fromapp=cctvnews&version=805&allow_comment=1&allow_comment=1
  40. http://www.qstheory.cn/zt2019/llxjj/index.htm
  41. http://www.cyberpolice.cn/wfjb/
  42. http://baijiahao.baidu.com/s?id=1670250874637091231
  43. http://baijiahao.baidu.com/s?id=1670239896280719334
  44. http://baijiahao.baidu.com/s?id=1670248053773599893
  45. http://image.baidu.com/
  46. http://baijiahao.baidu.com/s?id=1670243226621040644
  47. http://news.baidu.com/
  48. http://tieba.baidu.com/
  49. http://wenku.baidu.com/
  50. http://report.12377.cn:13225/toreportinputNormal_anis.do
  51. http://www.xinhuanet.com/politics/2020-06/23/c_1126149333.htm
  52. http://app.cctv.com/special/cportal/detail/arti/index.html?id=ArtiA1FM8grjZNDdJ15XVvv8200623&fromapp=cctvnews&version=727
  53. http://downpack.baidu.com/baidunews_AndroidPhone_1014720b.apk
  54. http://www.bjjubao.org/
  55. http://www.qstheory.cn/zt2017/xcgcdd19djs/index.htm
  1. li=[1,2,2,3,4,3,6,4,3]
  2. s=set(li) #set(list) 去重变为集合
  3. print(s)
  1. {1, 2, 3, 4, 6}
  1. # 递归 抓取所有的链接及链接页面的链接...:
  2. import requests
  3. from requests import exceptions
  4. from bs4 import BeautifulSoup
  5. pages=set()
  6. def geturl(url):
  7. global pages
  8. headers = {'User-Agent': 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
  9. # 捕获异常
  10. try:
  11. html = requests.get(url,headers=headers)
  12. except exceptions.ConnectionError:
  13. # print(url)
  14. print("连接错误")
  15. else:
  16. bs = BeautifulSoup(html.text, 'html.parser')
  17. links=set(bs.find_all('', {'href':re.compile('^(http://)|^(https://)')})) # set去重
  18. if links:
  19. for link in links:
  20. if link.attrs['href'] not in pages: # 去重
  21. pages.add(link.attrs['href'])
  22. print(link.attrs['href'])
  23. geturl(link.attrs['href']) # 递归
  24. else:
  25. print("已爬完!")
  26. home_link='http://www.baidu.com'
  27. geturl(home_link)
  28. print('end....')
  1. https://wenku.baidu.com
  2. https://www.baidu.com/cache/icon/favicon.ico
  3. 已爬完!
  4. https://www.baidu.com/cache/icon/favicon.svg
  5. 已爬完!
  6. https://jingyan.baidu.com
  7. https://passport.baidu.com/v2/?reg&tpl=exp&u=http%3A%2F%2Fjingyan.baidu.com%2F
  8. https://www.baidu.com/favicon.ico
  9. 已爬完!
  10. https://www.baidu.com/img/baidu.svg
  11. 已爬完!
  12. https://passport.baidu.com/v2/?ucenterfeedback#reg
  13. http://www.baidu.com/
  14. https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F&sms=5
  15. https://passport.baidu.com/export/app/index.html
  16. https://downpack.baidu.com/ppSecurityCenter_AndroidPhone_passport.apk
  17. 已爬完!
  18. https://itunes.apple.com/cn/app/bai-du-quan-zhong-xin-shou/id695439229
  19. https://www.apple.com.cn/iphone/
  20. https://www.apple.com/kw/iphone/
  21. https://www.apple.com/lae/iphone/
  22. https://www.apple.com/gn/iphone/
  23. https://support.apple.com/fr-gn
  24. https://support.apple.com/ko-kr
  25. https://support.apple.com/en-al
  26. https://support.apple.com/fr-sn
  27. https://support.apple.com/ru-ru
  28. https://www.apple.com/ru/
  29. https://www.apple.com/kr/
  30. https://www.apple.com/la/
  31. ---为了方便展示,删除几百行---
  32. KeyboardInterrupt:
  1. # 增强上面的代码:只爬取链接,往往是没有什么用的,现在我们增加点功能:
  2. # 1)所有链接网页的标题
  3. # h1-->span
  4. # 2)第一段文字
  5. # div#mw-content-text-->p
  6. # 3)编辑链接
  7. # li#cca-edit-->span-->a
  8. import requests
  9. from bs4 import BeautifulSoup
  10. from requests import exceptions
  11. import re
  12. pages = set()
  13. def geturl(url):
  14. global pages
  15. headers = {'User-Agent': 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
  16. # 捕获异常
  17. html = requests.get(url,headers=headers)
  18. bs = BeautifulSoup(html.text, 'html.parser')
  19. try:
  20. print(bs.h1)
  21. # print(bs.find(id ='mw-content-text').find_all('p')[0])
  22. # print(bs.find(id='ca-edit').find('span').find('a').attrs['href'])
  23. except AttributeError:
  24. print('This page is missing something! Continuing.')
  25. except exceptions.ConnectionError:
  26. print("连接错误")
  27. except exceptions.HTTPError:
  28. print('HTTP错误异常')
  29. except exceptions.MaxRetryError:
  30. print("多次重试失败")
  31. except exceptions.TooManyRedirects:
  32. print("多次重定位失败")
  33. except exceptions.ConnectTimeout:
  34. print("连接远程服务超时错误")
  35. except exceptions.Timeout:
  36. print("请求 URL超时")
  37. links=set(bs.find_all('', {'href':re.compile('^(http://)|^(https://)')})) # set去重
  38. if links:
  39. for link in links:
  40. if 'href' in link.attrs:
  41. if link.attrs['href'] not in pages:
  42. newPage = link.attrs['href']
  43. print('-'*20)
  44. print(newPage)
  45. pages.add(newPage)
  46. geturl(newPage)
  47. else:
  48. print("已爬完!")
  49. # home_link='https://baike.baidu.com/'
  50. home_link='https://baike.hk.xileso.top/wiki/Wikipedia:首页'
  51. geturl(home_link)
  52. print('end....')
  1. # 书上的例子(不翻墙没法访问,例子不可用)
  2. from urllib.request import urlopen
  3. from bs4 import BeautifulSoup
  4. import re
  5. pages = set()
  6. def getLinks(pageUrl):
  7. global pages
  8. html = urlopen('http://en.wikipedia.org{}'.format(pageUrl))
  9. bs = BeautifulSoup(html, 'html.parser')
  10. try:
  11. print(bs.h1.get_text())
  12. print(bs.find(id ='mw-content-text').find_all('p')[0])
  13. print(bs.find(id='ca-edit').find('span').find('a').attrs['href'])
  14. except AttributeError:
  15. print('This page is missing something! Continuing.')
  16. for link in bs.find_all('a', href=re.compile('^(/wiki/)')):
  17. if 'href' in link.attrs:
  18. if link.attrs['href'] not in pages:
  19. #We have encountered a new page
  20. newPage = link.attrs['href']
  21. print('-'*20)
  22. print(newPage)
  23. pages.add(newPage)
  24. getLinks(newPage)
  25. getLinks('')

Spider_实践_beautifulsoup静态网页爬取所有网页链接的更多相关文章

  1. 使用htmlparse爬虫技术爬取电影网页的全部下载链接

    昨天,我们利用webcollector爬虫技术爬取了网易云音乐17万多首歌曲,而且还包括付费的在内,如果时间允许的话,可以获取更多的音乐下来,当然,也有小伙伴留言说这样会降低国人的知识产权保护意识,诚 ...

  2. 使用htmlparser爬虫技术爬取电影网页的全部下载链接

    昨天,我们利用webcollector爬虫技术爬取了网易云音乐17万多首歌曲,而且还包括付费的在内,如果时间允许的话,可以获取更多的音乐下来,当然,也有小伙伴留言说这样会降低国人的知识产权保护意识,诚 ...

  3. R语言爬取动态网页之环境准备

    在R实现pm2.5地图数据展示文章中,使用rvest包实现了静态页面的数据抓取,然而rvest只能抓取静态网页,而诸如ajax异步加载的动态网页结构无能为力.在R语言中,爬取这类网页可以使用RSele ...

  4. 使用urllib进行网页爬取

    # coding=gbk # 抓取开奖号码 # url:http://datachart.500.com/dlt/zoushi/jbzs_foreback.shtml ''' 对网页逐行迭代,找到目标 ...

  5. python爬取某个网页的图片-如百度贴吧

    python爬取某个网页的图片-如百度贴吧 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用告诉我 #coding:utf-8 import urllib imp ...

  6. WebFetch 是无依赖极简网页爬取组件

    WebFetch 是无依赖极简网页爬取组件,能在移动设备上运行的微型爬虫. WebFetch 要达到的目标: 没有第三方依赖jar包 减少内存使用 提高CPU利用率 加快网络爬取速度 简洁明了的api ...

  7. 动态网页爬取例子(WebCollector+selenium+phantomjs)

    目标:动态网页爬取 说明:这里的动态网页指几种可能:1)需要用户交互,如常见的登录操作:2)网页通过JS / AJAX动态生成,如一个html里有<div id="test" ...

  8. 12月4日学习爬虫007.使用Urllib模块进行简单网页爬取

    笔记如下: 1.https是http加强版协议(安全协议)http(普通网络通信协议) 爬数据 如果爬https发现和理想中的数据不同,可以改为http 直接去掉s即可 2.使用Urllib爬取简单网 ...

  9. python使用requests库爬取网页的小实例:爬取京东网页

    爬取京东网页的全代码: #爬取京东页面的全代码 import requests url="https://item.jd.com/2967929.html" try: r=requ ...

随机推荐

  1. const、define 和 static 的区别

    目录 define.const static define.const 在 C++ 中,const 和 define 都可以用来定义常量.但是这二者之间有很大的区别: define 的作用 用 def ...

  2. firewalld和iptables区别

    在RHEL7里有几种防火墙共存:firewalld.iptables.ebtables,默认是使用firewalld来管理netfilter子系统,不过底层调用的命令仍然是iptables等. fir ...

  3. navicat 生成注册码( 仅供学习使用 )

    前言,由于navicat使用比较顺手,刚好前段时间试用期到,又看看了怎么生成注册码,特地记录下使用 . 1.运行 找到 navicat 文件(exe) 2.生成注册文件(报错好,后续会用到) 3.断网 ...

  4. S3C6410中断系统

    S3C6410的中断主要改进是. 增加中断向量控制器,这样在S3C2440里需要用软件来跳转的中断处理机制,在S3C6410完全由硬件来跳转.你只要把ISR地址是存在连续向量寄存器空间,而不是象S3C ...

  5. 多测师讲解接口测试 _fiddler无法打开浏览器_高级讲师肖sir

    错误现象: 打开fi'd'dler以后浏览器无法打开,关闭fiddler之后可以打开浏览器 解决方案: 1. 2. 在点击确定

  6. SpringBoot+Activiti+bpmn.js+Vue.js+Elementui(OA系统审批流)

    引言:OA系统用到请假.加班.调休.离职,需要使用工作流进行流程审批 一:activiti流程设计器的选择(通过学习activiti工作流过程中,发现一款好的流程设计器将会更好的方便的设计好流程(主要 ...

  7. elk-日志方案--使用Filebeat收集日志并输出到Kafka

      1,Filebeat简介 Filebeat是一个使用Go语言实现的轻量型日志采集器.在微服务体系中他与微服务部署在一起收集微服务产生的日志并推送到ELK. 在我们的架构设计中Kafka负责微服务和 ...

  8. k8s-获取kuboardtoken

    master节点执行命令 echo $(kubectl -n kube-system get secret $(kubectl -n kube-system get secret | grep kub ...

  9. centos使用上一条命令的快捷键

    使用上一条的最后一个参数 有时需要连续多个命令操作一个路径很长的文件: cat /usr/share/doc/centos-release/GPL 下一个命令可能还要使用这个路径,即使有命令补全也会很 ...

  10. Hbase实用技巧:全量+增量数据的迁移方法

    摘要:本文介绍了一种Hbase迁移的方法,可以在一些特定场景下运用. 背景 在Hbase使用过程中,使用的Hbase集群经常会因为某些原因需要数据迁移.大多数情况下,可以跟用户协商用离线的方式进行迁移 ...