我今天弄了一下午修改这个代码,最后还是弄好了.原因是正则表达式的筛选不够准确,有时候是会带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. Zookeeper 介绍翻译

    源网址链接 https://zookeeper.apache.org/ Apache Zookeeper 开放源码的服务器,提供高可靠的分布式协调服务. Zookeeper是一个维护配置信息,命名服务 ...

  2. 0_Simple__simpleLayeredTexture

    二维分层纹理 ▶ 源代码.用纹理方法把元素按原顺序从 CUDA3D 数组中取出来,求个相反数再加上层数放入全局内存,输出. #include <stdio.h> #include &quo ...

  3. Window Mysql 5.7.18安装

    1:下载地址 https://dev.mysql.com/downloads/mysql/ 点击Community , 然后点击Community Server 位置, 下载 安装包 2: 配置环境变 ...

  4. as3 区别中文 英文 数字

    1)英文a-z是65-90,A-Z是97-112 2)数字是0-9是,48-57 3)上万的都是中文字符 var str:String = "hello world! 你好世界! 88!&q ...

  5. DrawDib 使用例子<转>

    #include<vfw.h>#pragma comment(lib,"Vfw32.lib") BITMAPINFOHEADER biHeader; memset(&a ...

  6. python之内置函数:map ,filter ,reduce总结

    map函数: #处理序列中的每个元素,得到的结果是一个'列表',该列表元素个数及位置与原来一样 filter函数: #遍历序列中的每个元素,判断每个元素得到一个布尔值,如果是true,则留下来 peo ...

  7. pandas datafram重命名列名称

    方法一: 直接给column赋值 df.columns=["a", "b"], 所有的column全部重命名 example: import pandas as ...

  8. 进入一个docker容器

    Starting from Docker 1.3 you can use Docker exec to enter a Docker container : docker exec -it CONTA ...

  9. 关于EL表达式取值的问题

    EL表达式取值时,如果没有指定作用域,EL表达式会自动按照作用域的大小,从小到大依次去找;比如${s},会自动按照"pageContext,request,session,applicati ...

  10. Cache Server

    [Cache Server] Whenever a source Asset like a .psd or an .fbx file is modified, Unity detects the ch ...