zoukankan      html  css  js  c++  java
  • 用requests库和BeautifulSoup4库爬取新闻列表

    
    

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


    import
    requests from bs4 import BeautifulSoup url='http://news.gzcc.cn/html/xiaoyuanxinwen/' res=requests.get(url) 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 time=news.select('.news-list-info')[0].contents[0].text tag=news.select('.news-list-description')[0].text url=news.select('a')[0]['href'] thumb=news.select('.news-list-thumb')[0].contents[0] print(time,title,url,tag,thumb)

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

    import requests
    from bs4 import BeautifulSoup
    
    url='http://gz.58.com/ershouche/?utm_source=market&spm=b-31580022738699-pe-f-829.hao360_101&PGTID=0d100000-0000-3ada-ba49-dc2ff301240a&ClickID=1'
    res=requests.get(url)
    res.encoding='utf-8'
    soup=BeautifulSoup(res.text,'html.parser')
    for esc in soup.select('dd'):
        if len(esc.select('a'))>0:
            url=esc.select('a')[0]['href']
            title=esc.select('a')[0].text
            print(title,url)

  • 相关阅读:
    修改MySQL数据文件的位置
    服务名无效。请键入 NET HELPMSG 2185 以获得更多的帮助。
    索引的讲解
    运行php程序时,浏览器跳出打开和保存提示框
    ora-4031错误
    MySQL_Oracle_事物的隔离级别
    GNU与Linux
    计算机网络_第一章
    Linux网卡高级命令
    Linux RAID简介
  • 原文地址:https://www.cnblogs.com/lqy-36/p/7601599.html
Copyright © 2011-2022 走看看