• 用requests库和BeautifulSoup4库爬取新闻列表


     1、用requests库和BeautifulSoup4库,爬取校园新闻列表的时间、标题、链接、来源。

    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')
    
    for news in soup.select('li'):
        if len(news.select('.news-list-title'))>0:
          title=news.select('.news-list-title')[0].text
          url=news.select('a')[0]['href']
          time=news.select('.news-list-info')[0].contents[0].text
          laiyuan=news.select('.news-list-info')[0].contents[1].text
          print(title,url,time,laiyuan)

    2、选一个自己感兴趣的主题,做类似的操作,为“爬取网络数据并进行文本分析”做准备。

    import requests
    from bs4 import BeautifulSoup
    res=requests.get('http://trips.tuniu.com/search?q=%E7%8F%A0%E6%B5%B7')
    res.encoding='utf-8'
    soup=BeautifulSoup(res.text,'html.parser')
    
    for trips in soup.select('li'):
        if len(trips.select('.list-name'))>0:
          title=trips.select('.list-name')[0].text
          url=trips.select('a')[0]['href']
          print(title,url)

  • 相关阅读:
    The Tamworth Two chapter 2.4
    USACO Controlling Companies chapter 2.3 已跪
    非递归快排
    链表二路归并
    Money Systems chapter 2.3 dp
    #pragma pack与sizeof union
    快慢指针
    12
    11
    10
  • 原文地址:https://www.cnblogs.com/chenyuanzhao/p/7600167.html
Copyright © 2020-2023  润新知