美文网首页
解析百度首页,并提取相应内容

解析百度首页,并提取相应内容

作者: XY_QL | 来源:发表于2020-02-07 23:51 被阅读0次

```python

import requests

import pprint

url='http://www.baidu.com'

html=requests.get(url)

html=html.content.decode(encoding='utf-8')

html

```

    '<!DOCTYPE html>\r\n<!--STATUS OK--><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><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write(\'<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=\'+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ \'" name="tj_login" class="lb">登录</a>\');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\r\n'

```python

from bs4 import BeautifulSoup

```

```python

bs=BeautifulSoup(html, "lxml")

print(bs.prettify())

```

    <!DOCTYPE html>

    <!--STATUS OK-->

    <html>

    <head>

      <meta content="text/html;charset=utf-8" http-equiv="content-type"/>

      <meta content="IE=Edge" http-equiv="X-UA-Compatible"/>

      <meta content="always" name="referrer"/>

      <link href="http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css" rel="stylesheet" type="text/css"/>

      <title>

      百度一下,你就知道

      </title>

    </head>

    <body link="#0000cc">

      <div id="wrapper">

      <div id="head">

        <div class="head_wrapper">

        <div class="s_form">

          <div class="s_form_wrapper">

          <div id="lg">

            <img height="129" hidefocus="true" src="//www.baidu.com/img/bd_logo1.png" width="270"/>

          </div>

          <form action="//www.baidu.com/s" class="fm" id="form" name="f">

            <input name="bdorz_come" type="hidden" value="1"/>

            <input name="ie" type="hidden" value="utf-8"/>

            <input name="f" type="hidden" value="8"/>

            <input name="rsv_bp" type="hidden" value="1"/>

            <input name="rsv_idx" type="hidden" value="1"/>

            <input name="tn" type="hidden" value="baidu"/>

            <span class="bg s_ipt_wr">

            <input autocomplete="off" autofocus="" class="s_ipt" id="kw" maxlength="255" name="wd" value=""/>

            </span>

            <span class="bg s_btn_wr">

            <input class="bg s_btn" id="su" type="submit" value="百度一下"/>

            </span>

          </form>

          </div>

        </div>

        <div id="u1">

          <a class="mnav" href="http://news.baidu.com" name="tj_trnews">

          新闻

          </a>

          <a class="mnav" href="http://www.hao123.com" name="tj_trhao123">

          hao123

          </a>

          <a class="mnav" href="http://map.baidu.com" name="tj_trmap">

          地图

          </a>

          <a class="mnav" href="http://v.baidu.com" name="tj_trvideo">

          视频

          </a>

          <a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">

          贴吧

          </a>

          <noscript>

          <a class="lb" href="http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1" name="tj_login">

            登录

          </a>

          </noscript>

          <script>

          document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');

          </script>

          <a class="bri" href="//www.baidu.com/more/" name="tj_briicon" style="display: block;">

          更多产品

          </a>

        </div>

        </div>

      </div>

      <div id="ftCon">

        <div id="ftConw">

        <p id="lh">

          <a href="http://home.baidu.com">

          关于百度

          </a>

          <a href="http://ir.baidu.com">

          About Baidu

          </a>

        </p>

        <p id="cp">

          ©2017 Baidu

          <a href="http://www.baidu.com/duty/">

          使用百度前必读

          </a>

          <a class="cp-feedback" href="http://jianyi.baidu.com/">

          意见反馈

          </a>

          京ICP证030173号

          <img src="//www.baidu.com/img/gs.gif"/>

        </p>

        </div>

      </div>

      </div>

    </body>

    </html>

```python

bs_f=bs.find_all('a',attrs={'class':'mnav'})

bs_f

```

    [<a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>,

    <a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>,

    <a class="mnav" href="http://map.baidu.com" name="tj_trmap">地图</a>,

    <a class="mnav" href="http://v.baidu.com" name="tj_trvideo">视频</a>,

    <a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">贴吧</a>]

```python

for i in range(0,len(bs_f)):

    print (bs_f[i].get_text())

```

    新闻

    hao123

    地图

    视频

    贴吧

```python

title_content=bs.find_all('title')

title_content

title_content[0].get_text()

```

    '百度一下,你就知道'

```python

all_a=bs.find_all('a')

all_a

for i in range(0,len(all_a)):

    print (all_a[i].get_text())

```

    新闻

    hao123

    地图

    视频

    贴吧

    登录

    更多产品

    关于百度

    About Baidu

    使用百度前必读

    意见反馈

```python

all_herf=bs.find_all('a')

for item in all_herf:

    print (item.get("href"))

```

    http://news.baidu.com

    http://www.hao123.com

    http://map.baidu.com

    http://v.baidu.com

    http://tieba.baidu.com

    http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1

    //www.baidu.com/more/

    http://home.baidu.com

    http://ir.baidu.com

    http://www.baidu.com/duty/

    http://jianyi.baidu.com/

```python

import xlwings as xla

dir(xla)

```

    ['App',

    'Book',

    'Chart',

    'Name',

    'Picture',

    'Range',

    'RangeColumns',

    'RangeRows',

    'Shape',

    'ShapeAlreadyExists',

    'Sheet',

    '__builtins__',

    '__cached__',

    '__doc__',

    '__file__',

    '__loader__',

    '__name__',

    '__package__',

    '__path__',

    '__spec__',

    '__version__',

    '_xlwindows',

    'apps',

    'arg',

    'books',

    'constants',

    'conversion',

    'expansion',

    'func',

    'get_udf_module',

    'import_udfs',

    'main',

    'ret',

    'serve',

    'server',

    'sheets',

    'sub',

    'sys',

    'time_types',

    'udfs',

    'utils',

    'view',

    'wraps',

    'xlplatform']

```python

```

相关文章

网友评论

      本文标题:解析百度首页,并提取相应内容

      本文链接:https://www.haomeiwen.com/subject/wjdbxhtx.html