requests实例1:京东商品页面的爬取

京东某商品的页面爬取:

全代码如下(使用通用框架进行爬取):

1 import requests
2 url = "https://item.jd.com/2967929.html"
3 try:
4     r = requests.get(url)
5     r.raise_for_status()
6     r.encoding = r.apparent_encoding
7     print(r.text[:1000])
8 except:
9     print("爬取失败")

输出:

 1 <!DOCTYPE HTML>
 2 <html lang="zh-CN">
 3 <head>
 4     <!-- shouji -->
 5     <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
 6     <title>【华为荣耀8】荣耀8 4GB+64GB 全网通4G手机 魅海蓝【行情 报价 价格 评测】-京东</title>
 7     <meta name="keywords" content="HUAWEI荣耀8,华为荣耀8,华为荣耀8报价,HUAWEI荣耀8报价"/>
 8     <meta name="description" content="【华为荣耀8】京东JD.COM提供华为荣耀8正品行货,并包括HUAWEI荣耀8网购指南,以及华为荣耀8图片、荣耀8参数、荣耀8评论、荣耀8心得、荣耀8技巧等信息,网购华为荣耀8上京东,放心又轻松" />
 9     <meta name="format-detection" content="telephone=no">
10     <meta http-equiv="mobile-agent" content="format=xhtml; url=//item.m.jd.com/product/2967929.html">
11     <meta http-equiv="mobile-agent" content="format=html5; url=//item.m.jd.com/product/2967929.html">
12     <meta http-equiv="X-UA-Compatible" content="IE=Edge">
13     <link rel="canonical" href="//item.jd.com/2967929.html"/>
14         <link rel="dns-prefetch" href="//misc.360buyimg.com"/>
15     <link rel="dns-prefetch" href="//static.360buyimg.com"/>
16     <link rel="dns-prefetch" href="//img10.360buyimg.com"/>
17     <link rel="dns

猜你喜欢

转载自www.cnblogs.com/Shiko/p/10827686.html