基于Python爬虫代码指定页面爬取新闻图片,新闻标题,新闻时间,新闻关键词,跟帖数。 网站:https://news.163.com/domestic/
时间: 2024-06-09 21:09:33 浏览: 15
好的,以下是基于Python的爬虫代码,可以爬取网易新闻国内版的新闻图片、标题、时间、关键词和跟帖数:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://news.163.com/domestic/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
news_list = soup.select('.news_default li')
for news in news_list:
img_url = news.select('img')[0]['src']
title = news.select('a')[0].text
time = news.select('.time')[0].text
keywords = news.select('.keywords')[0].text
comments = news.select('.cmt')[0].text
print('标题:', title)
print('时间:', time)
print('关键词:', keywords)
print('跟帖数:', comments)
print('图片链接:', img_url)
```
这段代码使用了requests库和BeautifulSoup库来解析网页,并通过CSS选择器来提取新闻的各项信息。你可以根据需要对代码进行修改和优化,例如加入数据存储和异常处理等功能。