Python3从搜狐国际新闻抓取---尝试版

Python3从搜狐国际新闻抓取—尝试版- 依赖包版本信息Python 3.7.2requests 2.21.0beautifulsoup4 4.6.3首先下载HTML代码到本地import requestsfrom bs4 import BeautifulSoupurl = 'http://www.sohu.com/c/8/1461'res = reque...
摘要由CSDN通过智能技术生成

Python3从搜狐国际新闻抓取—尝试版

点击此处获取完整代码

- 依赖包版本信息

Python 3.7.2
requests 2.21.0
beautifulsoup4 4.6.3

首先下载HTML代码到本地
import requests
from bs4 import BeautifulSoup
url = 'http://www.sohu.com/c/8/1461'
res = requests.get(url)
res.encoding = 'utf-8'
print(res.text)
  • 观察HTML也代码结构以及想要爬取的位置
使用BeautifulSoup解析
soup = BeautifulSoup(res.text, 'html.parser')
  • 此处可以使用print(soup.text)查看
# data-role="news-item"
# <div class="news-box clear news-box-aa">
# 上述class为多值的属性(空格隔开)
model = soup.select('.news-box-aa')
print(type(model))
print(model)
  • 此处model的类型为:<class ‘list’>,一个列表。
  • model[i],存放着每一条新闻的标题,以及详情链接
print(type(model[0]))
print(model[0])
  • 可以看到model列表的第一个元素的类型和值
  • type(model[0]) 为<class ‘bs4.element.Tag’>
  • 结果如下:
<div class="new
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值