使用Beautiful Soup抽取新闻的标题、内容、时间。
时间: 2024-06-10 22:03:48 浏览: 12
好的,我可以为您解答这个问题。使用Beautiful Soup可以很方便地提取新闻网站中的标题、内容和时间信息。以下是代码示例:
```python
import requests
from bs4 import BeautifulSoup
# 指定新闻网站的链接,这里以新浪新闻为例
url = 'https://news.sina.com.cn/'
# 发送 HTTP 请求,抓取网页内容
response = requests.get(url)
# 使用 BeautifulSoup 解析网页内容
soup = BeautifulSoup(response.text, 'html.parser')
# 提取新闻标题和链接
news_titles = soup.find_all('a', class_='news-link')
for title in news_titles:
print('标题:', title.text)
print('链接:', title.get('href'))
# 提取新闻时间
news_times = soup.find_all('span', class_='time')
for time in news_times:
print('时间:', time.text)
```
请注意,这个代码示例在提取新闻标题和链接时限定了元素的 class 属性为 'news-link'。在实际使用中,您需要根据实际情况调整参数。同样地,提取新闻时间时也要限定元素的 class 属性。
相关问题
使用Beautiful Soup抽取新闻的标题、内容、时间。保存在sqlite数据库文件中
以下是使用Beautiful Soup和sqlite3抽取新闻的标题、内容、时间并保存至sqlite数据库的Python代码示例:
```python
import requests
from bs4 import BeautifulSoup
import sqlite3
# 创建一个sqlite数据库连接
conn = sqlite3.connect('news.db')
c = conn.cursor()
# 创建一个表来保存新闻标题、内容、时间
c.execute('''CREATE TABLE IF NOT EXISTS news
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
content TEXT,
time TEXT)''')
# 爬取新闻网站的页面内容
url = 'https://news.sina.com.cn/'
res = requests.get(url)
res.encoding = 'utf-8'
html = res.text
# 使用Beautiful Soup解析网页内容
soup = BeautifulSoup(html, 'html.parser')
# 抽取新闻标题、内容、时间并保存到数据库中
for news in soup.select('.news-item'):
title = news.select('.news-title')[0].text.strip()
content = news.select('.news-content')[0].text.strip()
time = news.select('.time')[0].text.strip()
c.execute("INSERT INTO news (title, content, time) VALUES (?, ?, ?)", (title, content, time))
# 提交更改并关闭数据库连接
conn.commit()
conn.close()
```
运行代码后,将会在当前目录下生成一个news.db的sqlite数据库文件,其中包含了新闻的标题、内容、时间信息。您可以使用sqlite3命令行工具或其他sqlite客户端工具来查看和操作这个数据库文件。
使用Beautiful Soup抽取新闻的标题、内容、时间。保存在sqlite数据库文件中。
以下是使用Beautiful Soup抽取新闻的标题、内容、时间并保存在sqlite数据库文件中的代码:
```python
import requests
from bs4 import BeautifulSoup
import sqlite3
# 获取新闻页面的HTML代码
url = 'https://news.sina.com.cn/china/'
response = requests.get(url)
html = response.text
# 创建BeautifulSoup对象
soup = BeautifulSoup(html, 'html.parser')
# 获取新闻标题、内容、时间,并存入数据库
conn = sqlite3.connect('news.db')
cursor = conn.cursor()
for news in soup.select('.news-item'):
title = news.select('.news-item-title a')[0].text
content_url = news.select('.news-item-title a')[0]['href']
content_response = requests.get(content_url)
content_html = content_response.text
content_soup = BeautifulSoup(content_html, 'html.parser')
content = content_soup.select('.article')[0].text
time = news.select('.time')[0].text
cursor.execute("INSERT INTO news (title, content, time) VALUES (?,?,?)", (title, content, time))
conn.commit()
conn.close()
```
以上代码将新闻标题、内容、时间存入名为`news.db`的sqlite数据库文件中,其中数据库表格结构为:
```sql
CREATE TABLE news (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
content TEXT,
time TEXT
);
```
每次运行代码时,将会把新抓取的新闻数据存入数据库中。如果需要查看数据库中的数据,可以使用sqlite3命令行工具或其他sqlite客户端进行操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)