惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
Vercel News
Vercel News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Martin Fowler
Martin Fowler
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - Franky
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
H
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog

博客园 - 石曼迪

.net core web api 发布填坑 Visual Studio 2026 开发 MAUI app 记录 Windows下搭建rustdesk远程服务 nginx 二级域名配置 .NET CORE 相关配置 frps-frpc配置 使用SuperSocket开发联网斗地主(四):出牌 使用SuperSocket开发联网斗地主(三):抢地主 使用SuperSocket开发联网斗地主(二):发牌 使用SuperSocket开发联网斗地主(一):联网 SuperSocket服务端与客户端搭建 EXCEL脚本收藏 小程序开发记录 Python基础总结 在centos7.6上部署.netcore 3.0 web程序 最简单的centos上安装Nginx办法 七牛网数据备份 solr搜索分词优化 Ionic异常及解决 狗日的腾讯
Python练习
石曼迪 · 2020-02-03 · via 博客园 - 石曼迪

爬虫基础练习——抓取网页数据

题目:抓取http://www.cntour.cn/首页新闻

分析:依次找到要抓取的数据的节点

image

使用筛选器依次找到要抓取的节点

#main>div>div.mtop.firstMod.clearfix>div.centerBox>ul.newsList>li>a

然后代码如下:

import requests        #导入requests包
import re
from bs4 import    BeautifulSoup
url='http://www.cntour.cn/'
strhtml=requests.get(url)
soup=BeautifulSoup(strhtml.text,'lxml')
data = soup.select('#main>div>div.mtop.firstMod.clearfix>div.centerBox>ul.newsList>li>a')
for item in data:
    result={
        'ID':re.findall('\d+',item.get('href')),
        'title':item.get_text(),
        'link':item.get('href')
    }
    print(result)

结果如下:

image