python爬虫学习笔记

下载requests库,命令行pip install requests即可安装。命令行爬取京东实例:

>>> import requests
>>> r=requests.get("https://item.jd.com/3133813.html")
>>> r.status_code
200
>>> r.encoding
'gbk'
>>> r.text[:1000]
'<!-- shouji -->\n<!DOCTYPE HTML>\n<html lang="zh-CN">\n<head>\n    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />\n    <title>【AppleiPhone7】Apple iPhone 7 (A1660) 32G 玫瑰金色 移动联通电信4G手机【行情 报价 价格 评测】-京东</title>\n    <meta name="keywords" content="AppleiPhone7,AppleiPhone7,AppleiPhone7报价,AppleiPhone7报价"/>\n    <meta name="description" content="【AppleiPhone7】京东JD.COM提供AppleiPhone7正品行货,并包括AppleiPhone7网购指南,以及AppleiPhone7图片、iPhone7参数、iPhone7评论、iPhone7心得、iPhone7技巧等信息,网购AppleiPhone7上京东,放心又轻松" />\n    <meta name="format-detection" content="telephone=no">\n    <meta http-equiv="mobile-agent" content="format=xhtml; url=//item.m.jd.com/product/3133813.html">\n    <meta http-equiv="mobile-agent" content="format=html5; url=//item.m.jd.com/product/3133813.html">\n    <meta http-equiv="X-UA-Compatible" content="IE=Edge">\n    <link rel="canonical" href="//item.jd.com/3133813.html"/>\n        <link rel="dns-prefetch" href="//misc.360buyimg.com"/>\n    <link rel="dns-prefetch" href="//static.360buyi'
>>>

其中status_code返回200表示链接成功

import requests

url="www.baidu.com"

try:

 r=requests.get(url)

r.raise_for_status()

r.encoding=r.apparent_encoding

print(r.text[:1000])

except:

print("爬取失败")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值