作业1.1.9

import requests
from bs4 import BeautifulSoup

#获取一个新闻网页的内容,返回新闻dict和图片list
def get_news(url):
    #获取页面内容
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'}
    res=requests.get(url,headers=headers)
    res.encoding=res.apparent_encoding
    #2.用BeautifulSoup提取标题、时间、作者、来源、正文、图片
    soup = BeautifulSoup(res.text,'html.parser')
    #标题
    new_title=soup.find('h1',class_='main-title').text
    print(new_title)
    #时间
    time=soup.select('span.date')[0].text
    print(time)
    #作者
    author=soup.find('p',class_='show_author').text
    print(author)
    #来源
    source=soup.select('.source')[0].text
    print(source)
    #正文
    work=soup.find('div',class_='article').text
    print(work)
    #图片
    pic=soup.find('div',class_='img_wrapper')
    print(pic)


if __name__=='__main__':
    url='http://news.sina.com.cn/c/2018-07-24/doc-ihftenhz3571650.shtml'
    get_news(url)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值