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

    laravel5.4安装的报错 [InvalidArgumentException] Could not find package laravle/installer at any version f ...

  2. WINdows常用监控相关

    参考网址: http://www.jb51.net/article/49986.htm 一.图新Shell下: 1.    最直观的:(在运行里面输入CMD,以下命令都是在CMD下输入的:) 输入 s ...

  3. Requests库入门

    安装: $ pip install requests Response对象的一些基本属性: Response.status_code 请求的返回状态,正常为200 Response.text 页面的字 ...

  4. jmeter java请求:java.lang.VerifyError: Cannot inherit from final class

    被这个问题block了一天,应该是包冲突的.通过对包删减排查,结果发现是netty-all-xxx.Final.jar包的问题 应该是jmeter版本和netty版本的冲突吧,换成jmeter 3.1 ...

  5. 下载google code中源码的几个工具

    Google code 一般以三种命令行方式提供源代码,格式如下: hg clone https://code.google.com/p/xxx/ git clone https://code.goo ...

  6. MFC-Dialog各函数的执行顺序

    CDlgTestDlg::CDlgTestDlg CDlgTestDlg::DoModal CDialog::DoModal CDlgTestDlg::PreSubclassWindow CDlgTe ...

  7. Ztree学习(-)简单例子

    https://www.cnblogs.com/shinhwazt/p/5828031.html ztree包:https://pan.baidu.com/s/1vOgGm_elF-lF0VowoHw ...

  8. WP runtime local setting

    https://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.localsettings. ...

  9. git基本命令之删除撤销操作

    1.将删除文件恢复--撤销所删除的文件git checkout 文件名 2.git resetgit reset --hard commitID(或某个节点)----强制切换到某个点,会导致所修改的内 ...

  10. 进程间通信-Queue

    进程间通信-Queue Process之间有时需要通信,操作系统提供了很多机制来实现进程间的通信. 1. Queue的使用 可以使用multiprocessing模块的Queue实现多进程之间的数据传 ...