成都创新互联网站制作重庆分公司

python如何访问网页

在Python中,有多种方法可以访问网页,其中最常用的方法是使用requests库和BeautifulSoup库,以下是详细的技术教学:

企业建站必须是能够以充分展现企业形象为主要目的,是企业文化与产品对外扩展宣传的重要窗口,一个合格的网站不仅仅能为公司带来巨大的互联网上的收集和信息发布平台,创新互联公司面向各种领域:公路钻孔机成都网站设计公司成都营销网站建设解决方案、网站设计等建站排名服务。


1、安装所需库

我们需要安装两个库:requests和BeautifulSoup,可以使用以下命令安装:

pip install requests
pip install beautifulsoup4

2、使用requests库访问网页

requests库是Python中一个非常常用的HTTP库,可以用来发送HTTP请求,以下是一个简单的示例,展示了如何使用requests库访问网页:

import requests
url = 'https://www.example.com'
response = requests.get(url)
print(response.text)

在这个示例中,我们首先导入了requests库,然后定义了一个URL变量,用于存储我们要访问的网页地址,接着,我们使用requests.get()函数发送一个GET请求到指定的URL,并将响应存储在response变量中,我们打印出响应的文本内容。

3、使用BeautifulSoup库解析网页内容

BeautifulSoup库是一个用于解析HTML和XML文档的Python库,它可以帮助我们从网页中提取所需的信息,以下是一个简单的示例,展示了如何使用BeautifulSoup库解析网页内容:

from bs4 import BeautifulSoup
import requests
url = 'https://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
提取所有的段落标签

paragraphs = soup.find_all('p') for p in paragraphs: print(p.text)

在这个示例中,我们首先导入了BeautifulSoup库和requests库,我们使用requests.get()函数发送一个GET请求到指定的URL,并将响应存储在response变量中,接着,我们使用BeautifulSoup()函数创建一个BeautifulSoup对象,将响应的文本内容作为参数传递给它,并指定解析器为’html.parser’,我们使用find_all()方法提取所有的段落标签

,并遍历它们,打印出每个段落的文本内容。

4、使用requests和BeautifulSoup库抓取网页数据

结合requests和BeautifulSoup库,我们可以抓取网页上的各种数据,以下是一个简单的示例,展示了如何使用这两个库抓取网页上的标题和正文内容:

from bs4 import BeautifulSoup
import requests
url = 'https://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
提取标题标签和<h1><h6>标签的内容
title = soup.title.string if soup.title else ''
headings = [tag.string for tag in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])]
subheadings = [tag.string for tag in soup.find_all(['h2', 'h3', 'h4', 'h5', 'h6'])] if soup.find_all(['h2', 'h3', 'h4', 'h5', 'h6']) else []
print('标题:', title)
print('一级标题:', headings)
print('二级标题:', subheadings)
</pre><p>在这个示例中,我们首先导入了BeautifulSoup库和requests库,我们使用requests.get()函数发送一个GET请求到指定的URL,并将响应存储在response变量中,接着,我们使用BeautifulSoup()函数创建一个BeautifulSoup对象,将响应的文本内容作为参数传递给它,并指定解析器为’html.parser’,我们分别提取了标题标签<title>、一级标题标签<h1><h6>和二级标题标签<h2><h6>的内容,并打印出来。</p><p>通过requests库和BeautifulSoup库,我们可以方便地访问和解析网页内容,这些库为我们提供了丰富的功能,可以帮助我们轻松地抓取网页上的各种数据,希望以上内容对你有所帮助!</p>            <br>
            网页名称:python如何访问网页            <br>
            分享地址:<a href="http://cxhlcq.com/article/djddods.html">http://cxhlcq.com/article/djddods.html</a>
        </div>
    </div>
</div>
<div class="other container">
    <h3>其他资讯</h3>
    <ul>
        <li>
                <a href="/article/cdscogs.html">这一次,我们带你搞懂看似神秘的Kubernetes!</a>
            </li><li>
                <a href="/article/cdscohh.html">盲人程序员是怎样炼成的</a>
            </li><li>
                <a href="/article/cdscdoo.html">Linux运维必备命令大全(linux运维命令大全)</a>
            </li><li>
                <a href="/article/cdscopc.html">python库的用法</a>
            </li><li>
                <a href="/article/cdscddh.html">订阅号和服务号哪个好,公众号一个月只能发4次吗</a>
            </li>    </ul>
</div>
<div class="footer">
    <div class="foota container">
        <div class="foot_nav fl col-lg-8 col-md-8 col-sm-12 col-xs-12">
            <ul>
                <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
                    <h3>网站制作</h3>
                    <a href="http://www.cxhljz.cn/" target="_blank">成都网站制作</a><a href="https://www.cdcxhl.com/" target="_blank">网站制作</a><a href="http://seo.cdkjz.cn/wangzhan/" target="_blank">网站制作公司</a><a href="https://www.cdxwcx.com/wangzhan/mbqiye.html" target="_blank">成都企业网站制作</a><a href="http://www.cdkjz.cn/" target="_blank">成都网站制作</a><a href="http://www.cqcxhl.com/" target="_blank">网站制作</a>                </li>
                <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
                    <h3>企业服务</h3>
                    <a href="https://www.cdcxhl.com/link/" target="_blank">链接买卖</a><a href="https://www.cdcxhl.com/mianfei/jianshe/chengdu.html" target="_blank">免费网站建设</a><a href="https://www.cdcxhl.com/service/ypfwzgz.html" target="_blank">互联网药品信息服务资格证</a><a href="https://www.cdcxhl.com/weihu/chengdu.html" target="_blank">成都网站维护</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">分类目录网站</a><a href="https://www.cdcxhl.com/link/" target="_blank">卖友情链接</a>                </li>
                <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
                    <h3>网站建设</h3>
                    <a href="http://seo.cdkjz.cn/yingxiao/" target="_blank">营销型网站建设</a><a href="http://www.cdxtjz.cn/" target="_blank">成都网站建设</a><a href="https://www.cdcxhl.com/qiye.html" target="_blank">企业网站建设</a><a href="http://www.cdkjz.cn/fangan/logistics/" target="_blank">物流公司网站建设方案</a><a href="http://www.cxjianzhan.com/mobile/" target="_blank">wap网站建设</a><a href="http://m.cdcxhl.cn/qiye/" target="_blank">成都企业网站建设</a>                </li>
                <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6">
                    <h3>服务器托管</h3>
                    <a href="https://www.cdcxhl.com/idc/yidong.html" target="_blank">移动服务器托管</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">成都机柜租用</a><a href="https://www.cdcxhl.com/idc/cqlt.html" target="_blank">重庆联通机房托管</a><a href="https://www.cdcxhl.com/idc/mianyang.html" target="_blank">绵阳服务器托管</a><a href="http://www.cqfuwuqi.com/" target="_blank">重庆服务器托管</a><a href="https://www.cdxwcx.com/jifang/meishan.html" target="_blank">眉山服务器托管</a>                </li>
            </ul>
        </div>
        <div class="footar fl col-lg-4 col-md-4 col-sm-12 col-xs-12">
            <p>全国免费咨询:</p>
            <b>400-028-6601</b>
            <p>业务咨询:028-86922220 / 13518219792</p>
            <p>节假值班:18980820575 / 13518219792</p>
            <p>联系地址:成都市太升南路288号锦天国际A幢1002号</p>
        </div>
    </div>
    <div class="footb">
        <div class="copy container">
            <div class="fl">Copyright © 成都创新互联科技有限公司重庆分公司  <a href="https://beian.miit.gov.cn/" target="_blank">渝ICP备2021005571号</a></div>
            <!--<div class="fr"><a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>:<a href="https://www.cdcxhl.com/" target="_blank">创新互联</a></div>-->
        </div>
    </div>
    <div class="link">
        <div class="container">
            友情链接::
            <a href="https://www.cdcxhl.com/" target="_blank">成都网站建设</a>
            <a href="https://www.cdcxhl.com/city/chongqing.html" target="_blank">重庆网站建设</a>
            <a href="">四川网站建设</a>
            <a href="">重庆建设网站</a>
            <a href="https://www.cdxwcx.com/jifang/xiyun.html" target="_blank">移动服务器托管</a>
            <a href="http://www.cdfuwuqi.com/" target="_blank">成都服务器托管</a>
            <a href="https://www.cdcxhl.cn/" target="_blank">云服务器</a>
            <a href="http://www.cdhuace.com/" target="_blank">广告设计制作</a>
            <a href="https://www.cdcxhl.com/sheji/chongqing.html" target="_blank">重庆网页设计</a>
            <a href="https://www.cdcxhl.com/zuo/chongqing.html" target="_blank">重庆做网站</a>
            <a href="https://www.cdcxhl.com/zhizuo/chongqing.html" target="_blank">重庆网站制作</a>
            <a href="">重庆网站建设</a>
            <a href="">重庆网站公司</a>
            <a href="">渝中网站制作</a>
            <a href="">重庆网站设计</a>
        </div>
    </div>
</div>
<div class="foot">
    <ul class="public-celan">
        <li>
            <a href="https://p.qiao.baidu.com/cps3/chatIndex?siteToken=6ce441ff9e2d6bedbdfc2a4138de449e&speedLogId=162260383240185e3_1622603832401_02407&eid=6256368&reqParam=%7B%22from%22%3A1%2C%22sessionid%22%3A%22-100%22%2C%22siteId%22%3A%2211284691%22%2C%22tid%22%3A%22-1%22%2C%22userId%22%3A%226256368%22%2C%22ttype%22%3A1%2C%22pageId%22%3A0%7D" target="_blank" class="a1 db tc">
                <img src="/Public/Home/img/icon-23.png" alt="" class="db auto">
                <span class="span-txt">在线咨询</span>
            </a>
        </li>
        <li>
            <a href="tel:18980820575" class="a1 db tc">
                <img src="/Public/Home/img/icon-24.png" alt="" class="db auto">
                <span class="span-txt">电话咨询</span>
            </a>
        </li>
        <li>
            <a target="_blank" href="tencent://message/?uin=1683211881&Site=&Menu=yes" class="a1 db tc">
                <img src="/Public/Home/img/icon-25.png" alt="" class="db auto">
                <span class="span-txt">QQ咨询</span>
            </a>
        </li>
        <li>
            <a target="_blank" href="tencent://message/?uin=532337155&Site=&Menu=yes" class="a1 db tc public-yuyue-up">
                <img src="/Public/Home/img/icon-26.png" alt="" class="db auto">
                <span class="span-txt">预约顾问</span>
            </a>
        </li>
    </ul>
</div>
<div class="customer">
    <dl class="icon1">
        <dt>
            <a href="tencent://message/?uin=1683211881&Site=&Menu=yes">
                <i class="iconT"><img src="/Public/Home/img/QQ.png" alt=""></i>
                <p>在线咨询</p>
            </a>
        </dt>
    </dl>
    <dl class="icon2">
        <dt><i><img src="/Public/Home/img/weixin.png" alt=""></i><p>微信咨询</p></dt>
        <dd><img src="/Public/Home/img/ewm.png"></dd>
    </dl>
    <dl class="icon3">
        <dt><i><img src="/Public/Home/img/dianhua.png" alt=""></i><p>电话咨询</p></dt>
        <dd>
            <p>028-86922220(工作日)</p>
            <p>18980820575(7×24)</p>
        </dd>
    </dl>
    <dl class="icon4">
        <dt class="sShow">
            <a href="tencent://message/?uin=244261566&Site=&Menu=yes">
                <i><img src="/Public/Home/img/dengji.png" alt=""></i><p>提交需求</p>
            </a>
        </dt>
    </dl>
    <dl class="icon5">
        <dt class="gotop">
            <a href="#top">
                <i><img src="/Public/Home/img/top.png" alt=""></i><p>返回顶部</p>
            </a>
        </dt>
    </dl>
</div>
</body>
</html>
<script>
    $(".con img").each(function(){
        var src = $(this).attr("src");    //获取图片地址
        var str=new RegExp("http");
        var result=str.test(src);
        if(result==false){
            var url = "https://www.cdcxhl.com"+src;    //绝对路径
            $(this).attr("src",url);
        }
    });
    window.onload=function(){
        document.oncontextmenu=function(){
            return false;
        }
    }
</script>