我今天弄了一下午修改这个代码,最后还是弄好了.原因是正则表达式的筛选不够准确,有时候是会带http:baidu这些东西的.所以需要一个正则表达式的断言,然后还有一点是如果his里面只有一个元素就不要再pop了.
最后我把代码放上去.为了突出问题我选的开始网址很特殊他没有子链接.
from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
import random

base_url = "https://baike.baidu.com"
his = ["/item/%E9%B8%A1%E7%9F%A2%E8%97%A4/931175?fromtitle=%E9%B8%A1%E5%B1%8E%E8%97%A4&fromid=374872"]

for i in range(200):
url = base_url + his[-1]

print (77777777)

html = urlopen(url).read().decode('utf-8')
print (8888888888)
soup = BeautifulSoup(html, features='lxml')
print (9999999)

print(i, soup.find('h1').get_text(), ' url: ',base_url+ his[-1])

# find valid urls
sub_urls = soup.find_all("a", {"target": "_blank", "href": re.compile("(?=^/item)/item/(%.{2})+$")})

print ('爬到了'+str(i+1))
if len(sub_urls) != 0 :

his.append(random.sample(sub_urls, 1)[0]['href'])
else:
if len(his)==1:
pass
else:
# no valid sub link found
his.pop()

#到底错在了哪里???为什么多跑几遍,很小概率可能会发生1.无限重复同一个url 2.死循环 3.当然也有可能无限2个url来回跳
print ('666666')
#已经修改好了,有错误可以继续回复我

莫烦python课程里面的bug修复;课程爬虫小练习爬百度百科的更多相关文章

  1. Python 爬虫实例(爬百度百科词条)

    爬虫是一个自动提取网页的程序,它为搜索引擎从万维网上下载网页,是搜索引擎的重要组成.爬虫从一个或若干初始网页的URL开始,获得初始网页上的URL,在抓取网页的过程中,不断从当前页面上抽取新的URL放入 ...

  2. 记一次如何解决低版本jar包里面的bug,不适宜替换成高版本的经历

    背景:目前正在迭代开发的项目,应用户要求新增一个电子文档转换的功能,即将不标准的excel文件转换为标准的excel文件(标准模板). 选择:pio ,本项目里面本来就有poi的jar包 问题:项目里 ...

  3. 莫烦python教程学习笔记——保存模型、加载模型的两种方法

    # View more python tutorials on my Youtube and Youku channel!!! # Youtube video tutorial: https://ww ...

  4. 莫烦python教程学习笔记——validation_curve用于调参

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

  5. 莫烦python教程学习笔记——learn_curve曲线用于过拟合问题

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

  6. 莫烦python教程学习笔记——利用交叉验证计算模型得分、选择模型参数

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

  7. 莫烦python教程学习笔记——数据预处理之normalization

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

  8. 莫烦python教程学习笔记——线性回归模型的属性

    #调用查看线性回归的几个属性 # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg # ...

  9. 莫烦python教程学习笔记——使用波士顿数据集、生成用于回归的数据集

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

随机推荐

  1. hadoop分布式快速搭建

    hadoop分布式快速搭建 1.配置主节点与从节点的ssh互信:[其中在主从节点主机的/etc/hosts文件中需绑定主机名ip间的映射关系; 如,192.168.1.113 node0 192.16 ...

  2. webserver有哪些

    http://blog.csdn.net/mfsh_1993/article/details/70245380 常用web服务器有Apache.Nginx.Lighttpd.Tomcat.IBM We ...

  3. FireDAC FDQuery

    http://docwiki.embarcadero.com/RADStudio/XE6/en/TFDMemTable_Questions#Q:_How_can_I_copy_all_records_ ...

  4. Procedure-Function mysql

    1.基本用法 drop PROCEDURE if EXISTS sp1; -- 如果存在sp1存储过程则删除掉 create PROCEDURE sp1() SELECT 1; --创建最简单的存储过 ...

  5. MIME(Multipurpose Internet Mail Extensions-多用途互联网邮件扩展)

    MIME MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型.是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时 ...

  6. HTTP状态码 解析

    原文地址:HTTP状态码:400500错误代码(个人总结)作者:lining2008net 一些常见的状态码为: 200-服务器成功返回网页 404-请求的网页不存在 503-服务不可用 详细分解: ...

  7. linux中与Oracle有关的内核参数详解

    工作当中遇到oracle运行时CPU占用率达到90%以上,调小以下参数值后恢复正常. fs.file-max = 65536 net.core.rmem_default=262144 net.core ...

  8. Becoming inspired - ASC - 2017 MARCH 24

    Becoming inspired - The 11 questions to ask yourself when you feel uninspired @ Advanced Studio Clas ...

  9. Virtualbox [The headers for the current running kernel were not found] (操作过程后还是失败,显示相同问题)

    在笔记本安装Ubuntu11.04增强功能失败 引用 fuliang@fuliang-VirtualBox:~$ sudo /etc/init.d/vboxadd setup Removing exi ...

  10. linux基础命令:

    linux基础命令: 显示 echo 输出我写的内容 ls 查看当前目录的文件 pwd 查看当前目录 ifconfig 查看网卡信息 grep 过滤 -v 取反 -n man 查看命令的帮助信息 md ...