澄梦希喜广告网站制作遂宁站

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://hfanp.com/article/djddods.html">http://hfanp.com/article/djddods.html</a>
        </div>
    </div>
</div>
<div class="other container">
    <h3>其他资讯</h3>
    <ul>
        <li>
                <a href="/article/dpedjip.html">中国电信云主机价格</a>
            </li><li>
                <a href="/article/dpedjdp.html">利用常用Redis工具类提高开发效率(常用redis工具类)</a>
            </li><li>
                <a href="/article/dpedjjj.html">什么是动态IP地址?动态ip服务器</a>
            </li><li>
                <a href="/article/dpedjpd.html">如何使用Linux查看网络物理连接?(linux查看网络物理连接)</a>
            </li><li>
                <a href="/article/dpedjpc.html">Oracle下一个目标真是AMD?</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.cdxwcx.cn/" target="_blank">成都网站制作</a><a href="http://m.cdcxhl.cn/dingzhi/" target="_blank">定制网站制作</a><a href="http://chengdu.cdcxhl.com/" target="_blank">成都网站制作</a><a href="https://www.cdcxhl.com/zhizuo/chengdu.html" target="_blank">四川成都网站制作</a><a href="http://chengdu.xwcx.net/mobile/" target="_blank">移动手机网站制作</a><a href="http://www.cdxwcx.cn/bj/" 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/service/shipinxuke.html" target="_blank">食品经营许可证</a><a href="https://www.cdcxhl.com/service/zzgj.html" target="_blank">备案资质管家服务</a><a href="https://www.cdcxhl.com/weihu/chengdu.html" target="_blank">成都网站维护</a><a href="https://www.cdcxhl.com/service/dailijizhang.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="https://www.cdxwcx.com/city/yaan/" target="_blank">雅安网站建设</a><a href="https://www.cdxwcx.com/city/mianzhu/" target="_blank">绵竹网站建设</a><a href="http://www.cdkjz.cn/fangan/logistics/" target="_blank">物流公司网站建设方案</a><a href="http://www.myzitong.cn/" target="_blank">绵阳梓潼网站建设</a><a href="http://www.sczitong.cn/" target="_blank">梓潼网站建设</a><a href="http://m.cdcxhl.com/liucheng.html" 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/zongshu.html" target="_blank">棕树服务器托管</a><a href="https://www.cdcxhl.com/idc/cqwld.html" target="_blank">重庆电信五里店机房托管</a><a href="https://www.cdcxhl.com/idc/leshan.html" target="_blank">乐山服务器托管</a><a href="https://www.cdcxhl.com/tuoguan/" target="_blank">IDC机房托管</a><a href="https://www.xwcx.net/" target="_blank">成都托管服务器</a><a href="https://www.cdcxhl.com/idc/gadx.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>联系地址:四川省遂宁市船山区德水北路56号</p>
        </div>
    </div>
    <div class="footb">
        <div class="copy container">
            <div class="fl">Copyright © 四川澄梦希喜广告有限公司  <a href="https://beian.miit.gov.cn/" target="_blank">蜀ICP备2025175960号-6</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/suining.html" target="_blank">遂宁网站建设</a>
            <a href="https://www.cdcxhl.com/idc/meishan.html">眉山服务器托管</a>
            <a href="https://www.cdcxhl.com/paiming/suining.html">遂宁seo排名</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/suining.html" target="_blank">遂宁网页设计</a>
            <a href="https://www.cdcxhl.com/zuo/suining.html" target="_blank">遂宁做网站</a>
            <a href="https://www.cdcxhl.com/zhizuo/suining.html" target="_blank">遂宁网站制作</a>
            <a href="https://www.cdcxhl.com/seo/suining.html">遂宁网站推广</a>
            <a href="https://www.cdcxhl.com/idc/deyang.html">德阳服务器托管</a>
            <a href="https://www.cdcxhl.com/zuyong/meishan.html">眉山服务器租用</a>
            <a href="https://www.cdcxhl.com/tuoguan/">成都服务器托管</a>
        </div>
    </div>
</div>
<div class="foot">
    <ul class="public-celan">
        <li>
            <a href="#" 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>