我的第一个网页爬虫(python)

1、环境:python 3.5.1  编辑器:pycharm

2、学习网站:http://python.jobbole.com/81336/

3、实战:

使用该页面中提到的语句,python 3.5.1报错,无法识别urllib2的库,如:

import urllib2
response = urllib2 . urlopen ( "http://www.baidu.com" )
print response . read ( )
无法识别urllib2的urlopen方法

解决办法:原来urllib2和urllib库已经合并变成urllib库,提醒:不同的python版本可能库不一样,需要关注库的变化情况

脚本优化(参考地址:http://www.imooc.com/article/15230):

from urllib import request
response = request.urlopen("http://www.baidu.com")
print(response.read().decode("utf-8"))

运行结果(简化版):

<html>
<head>
    
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta content="always" name="referrer">
    <meta name="theme-color" content="#2932e1">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" /> 
    <link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu.svg">


<link rel="dns-prefetch" href="//s1.bdstatic.com"/>
<link rel="dns-prefetch" href="//t1.baidu.com"/>
<link rel="dns-prefetch" href="//t2.baidu.com"/>
<link rel="dns-prefetch" href="//t3.baidu.com"/>
<link rel="dns-prefetch" href="//t10.baidu.com"/>
<link rel="dns-prefetch" href="//t11.baidu.com"/>
<link rel="dns-prefetch" href="//t12.baidu.com"/>
<link rel="dns-prefetch" href="//b1.bdstatic.com"/>
    
    <title>百度一下,你就知道</title>
    


<style id="css_index" index="index" type="text/css">html,body{height:100%}
html{overflow-y:auto}
body{font:12px arial;text-align:;background:#fff}
body,p,form,ul,li{margin:0;padding:0;list-style:none}
body,form,#fm{position:relative}
td{text-align:left}
img{border:0}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值