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

推荐订阅源

T
The Blog of Author Tim Ferriss
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
有赞技术团队
有赞技术团队
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
博客园_首页
Y
Y Combinator Blog
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
P
Proofpoint News Feed
B
Blog
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 石曼迪

.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