待爬取的20条新闻部分如下:

Python爬虫 百度新闻列表20条的标题、链接、日期


通过观察审查元素发现,标题,链接和时间都藏在class=result里面,一个页面共有20条新闻,只要循环20遍,每次取其中div的相应元素即可。

Python爬虫 百度新闻列表20条的标题、链接、日期

#coding:utf-8

#引入相关模块
import requests
from bs4 import BeautifulSoup

url = 

"http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=%E4%B8%8A%E6%B5%B7%E6%B5%B7%E4%BA%8B%E5%A4%A7%E5%AD%A6"

#请求搜索上海海事大学关键字新闻网页的URL,获取其text文本

response = requests.get(url)  #对获取到的文本进行解析
html = response.text
soup=BeautifulSoup(html,features='lxml')  #根据HTML网页字符串创建BeautifulSoup对象
news=soup.find_all('div', {"class": "result"})

for t in news:
    data = {
        "标题":t.find('a').text,
        "链接":t.find('a')['href'],
        "时间":t.find('p').get_text()
    }

    print(data)     

运行结果如下:

Python爬虫 百度新闻列表20条的标题、链接、日期

相关文章:

  • 2021-04-11
  • 2022-12-23
  • 2021-08-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-30
  • 2021-08-13
猜你喜欢
  • 2022-12-23
  • 2021-09-15
  • 2021-04-24
  • 2021-04-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案