python(一)网络爬取

        在爬取网页信息时,需要注意网页爬虫规范文件robots.txt

        eg:csdn的爬虫规范文件 csdn.net/robots.txt

User-agent: 
        下面的Disallow规则适用于所有爬虫(即所有用户代理)。星号*是一个通配符,表示“所有”。

Disallow:

        禁止爬虫访问的路径

1、首先下载python的相关类库

pip install requests
pip install beautifulsoup4

        requests 是一个http库,可以发送网络请求 。

        beautifulsoup4 主要用来解析html文档。

2、引入相关库 

import requests    
from bs4 import BeautifulSoup  

3、编写相关代码

url = 'https://www.....com'    
response = requests.get(url)    
  
html_content = response.text  
soup = BeautifulSoup(html_content, 'html.parser')  
  
titles = soup.select('h2') 
for title in titles:  
    print(title.text)

        url : 需要爬的页面路径

        response = requests.get(url)  发送get请求并接受

        html_content = response.text 取出页面主体

        soup = BeautifulSoup(html_content, 'html.parser')  由beautifulsoup对主体中的h5标签解析

        titles = soup.select('h2')   选择所有的h2标签

        最后循环遍历打印出所有h2 标签

4、测试

相关推荐

  1. 如何使用Python进行网页

    2024-03-29 10:34:03       40 阅读
  2. python—爬虫图片网页实例

    2024-03-29 10:34:03       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-29 10:34:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-29 10:34:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-29 10:34:03       82 阅读
  4. Python语言-面向对象

    2024-03-29 10:34:03       91 阅读

热门阅读

  1. python解决序列重叠问题

    2024-03-29 10:34:03       34 阅读
  2. SQL查询:如何在where条件中使用子查询

    2024-03-29 10:34:03       39 阅读
  3. 【期刊介绍】ICLR

    2024-03-29 10:34:03       38 阅读
  4. 抖音美女直播听小说项目全攻略【鹏哥创业】

    2024-03-29 10:34:03       156 阅读
  5. volatile关键字的作用、原理

    2024-03-29 10:34:03       42 阅读
  6. playbook的介绍、应用与实施

    2024-03-29 10:34:03       30 阅读
  7. String 类的常用方法都有那些?

    2024-03-29 10:34:03       40 阅读
  8. Dubbo负载均衡

    2024-03-29 10:34:03       35 阅读
  9. ES-LTR粗排模块

    2024-03-29 10:34:03       43 阅读
  10. Adipogen ZBP1单克隆抗体

    2024-03-29 10:34:03       38 阅读