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)

  • 相关阅读:
    SQL Server 配置管理器不见了
    SQL常用函数使用
    数据库优化
    关于百度地图js api的getCurrentPosition定位不准确的解决方法
    uni-app中使用外部字体
    不定高度,元素垂直居中
    css实现的鼠标悬浮提示
    导入
    下载
    百度地图在地图上标点显示数字
  • 原文地址:https://www.cnblogs.com/lqy-36/p/7601599.html
Copyright © 2011-2022 走看看