python网络爬虫day'1
来源:互联网 发布:秦时明月知乎 编辑:程序博客网 时间:2024/06/05 08:12
喵喵喵,写个博客希望能对自己有一些激励作用。
昨晚重装anaconda的时候把注册表里的账户名修改了,重启后win10账户无法登入,我又只有那一个账户。
大概意思就是我把整个C盘都给搞丢了?然后cmd也不能用,登入的模式无法保存修改,也就没法改回去。
解决方法是——在别的盘再装一个win10,然后修改,然后再格式化掉第二个系统。
吁。
然后装好anaconda2,我打开spyder的时候报错来着。
看起来是他要求nbconvert高于4.0版本,而我没有版本…
用pip安装了之后依旧报错。不过目前还不影响代码运行…哭哭。
然后跟着imooc的教程走了一点点爬虫的内容。感觉这个教程没讲清楚…
代码贴下来吧…平时还能复习一下。
# -*- coding: utf-8 -*-
"""
网页下载器:
下载网页内容
URLlib/requests
"""
import urllib2
import cookielib
print("hello world")
url = "http://www.baidu.com"
#直接使用urlopen实现下载
response1 = urllib2.urlopen(url)
#使用getcode()获取状态码,若为200则成功
print response1.getcode()
#read()为网页内容
print len(response1.read())
request = urllib2.Request(url)
#添加数据
#request.add_data('a','1')
#将爬虫伪装成一个叫做Mozilla的浏览器
request.add_header('User-Agent','Mozilla/5.0')
#参数改为request
response2 = urllib2.urlopen(request)
#使用getcode()获取状态码,若为200则成功
print response2.getcode()
#read()为网页内容
print len(response2.read())
#创建cookie容器
cj = cookielib.CookieJar()
#创建opener
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
#给URLlib2安装opener,URL库具备了cookie处理
response3 = urllib2.urlopen(url)
print response1.getcode()
print cj
#打印网页内容
print response3.read()
print '********************************'
"""
网页解析器:
提取有价值的数据,和新的URL列表
正则(模糊匹配)、BeautifulSoup(结构化解析为Document Object Model树)
"""
from bs4 import BeautifulSoup
html_doc="""
<div class="s_tab" id="s_tab"><b>网页</b><a href="http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=add_data" wdfield="word" onmousedown="return c({'fm':'tab','tab':'news'})">新闻</a><a href="http://tieba.baidu.com/f?kw=add_data&fr=wwwt" wdfield="kw" onmousedown="return c({'fm':'tab','tab':'tieba'})">贴吧</a><a href="http://zhidao.baidu.com/q?ct=17&pn=0&tn=ikaslist&rn=10&word=add_data&fr=wwwt" wdfield="word" onmousedown="return c({'fm':'tab','tab':'zhidao'})">知道</a><a href="http://music.baidu.com/search?fr=ps&ie=utf-8&key=add_data" wdfield="key" onmousedown="return c({'fm':'tab','tab':'music'})">音乐</a><a href="http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=add_data" wdfield="word" onmousedown="return c({'fm':'tab','tab':'pic'})">图片</a><a href="http://v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&ie=utf-8&word=add_data" wdfield="word" onmousedown="return c({'fm':'tab','tab':'video'})">视频</a><a href="http://map.baidu.com/m?word=add_data&fr=ps01000" wdfield="word" onmousedown="return c({'fm':'tab','tab':'map'})">地图</a><a href="http://wenku.baidu.com/search?word=add_data&lm=0&od=0&ie=utf-8" wdfield="word" onmousedown="return c({'fm':'tab','tab':'wenku'})">文库</a><a href="//www.baidu.com/more/" onmousedown="return c({'fm':'tab','tab':'more'})">更多»</a></div>
"""
#创建beautifulsoup对象,就有了DOM树
soup = BeautifulSoup(html_doc, #html文档字符串
'html.parser', #解析器
from_encoding='utf-8' #文档编码
)
#通过dom的名称、属性、文字,使用find_all()或find(name,attrs,string)查找
print '链接:'
links = soup.find_all('a')
for link in links:
print link.name,link['href'],link.get_text()
#还可通过引入re模块使用正则表达式获取内容
- python网络爬虫day'1
- [Python]网络爬虫1
- Python网络爬虫1
- 【Python爬虫1】网络爬虫简介
- python 网络爬虫入门 1
- Python网络爬虫(1)
- python 3.0 网络爬虫 1
- Python网络爬虫演示-1
- Python网络爬虫1 ---- windows下搭建爬虫框架scrapy
- [Python]网络爬虫(1):你要干什么?
- Python网络爬虫(1)获取网页
- 《用python写网络爬虫》笔记1
- 异步网络爬虫的Python实现(1)
- python网络爬虫(1)--抓取图片
- Python-网络爬虫之BeautifulSoup(1)
- python 网络爬虫代码
- Python实现网络爬虫
- Python实现网络爬虫
- Android Study 之 findViewById 变迁之路
- IP地址 网段的划分
- 图像识别中的深度学习
- Linux下安装OpenCV 3.0
- 【机房重构】-组合查询之模版模式
- python网络爬虫day'1
- 读0day攻击C++函数心得
- 2014年度计划
- 关于linux 内存重复释放的问题
- windows溢出保护原理与绕过方法概览(转自riusksk's blog(泉哥))
- linux(ubuntu)下安装深度学习框架caffe
- windbg断点
- Python!Python!
- windbg 内存操作