zoukankan      html  css  js  c++  java
  • 网络爬虫基础练习

    以下是练习使用URL链接:
    http://news.gzcc.cn/html/xiaoyuanxinwen/

    这里是使用了requests库和BeautifulSoup库来做爬虫练习,所以在使用前先安装好这两个库

    练习要求如下:

    • 取出h1标签的文本
    • 取出a标签的链接
    • 取出所有li标签的所有内容
    • 取出第2个li标签的a标签的第3个div标签的属性
    • 取出一条新闻的标题、链接、发布时间、来源
    import requests
    from bs4 import BeautifulSoup
    res = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')
    res.encoding = 'UTF-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    
    # 取出h1标签的文本
    for h1 in soup.find_all('h1'):
        print(h1.text)
    # 取出a标签的链接
    for a in soup.find_all('a'):
        print(a.attrs.get('href'))
    # 取出所有li标签的所有内容
    for li in soup.find_all('li'):
        print(li.contents)
    # 取出第2个li标签的a标签的第3个div标签的属性
    print(soup.find_all('li')[1].a.find_all('div')[2].attrs)
    
    # 取出一条新闻的标题、链接、发布时间、来源
    print(soup.select('div .news-list-title')[0].text)
    print(soup.select('div .news-list-thumb')[0].parent.attrs.get('href'))
    print(soup.select('div .news-list-info > span')[0].text)
    print(soup.select('div .news-list-info > span')[1].text)
    
    
  • 相关阅读:
    深度学习的优化算法
    基于双向的CNN的细粒度物体识别论文翻译
    LSTM公式推导
    结巴分词python脚本
    eval() python 中的
    C++编译原理
    extern,以及在linux头文件中的应用
    iostream源码
    LINUX命令
    apt-get
  • 原文地址:https://www.cnblogs.com/lger/p/8666073.html
Copyright © 2011-2022 走看看