帮我写一个爬取新闻标题和正文的代码
时间: 2023-08-20 07:34:25 浏览: 54
好的,你需要先安装requests、BeautifulSoup4和lxml这三个库。然后可以使用以下代码来爬取新闻标题和正文:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://news.sina.com.cn/china/'
response = requests.get(url)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'lxml')
news_list = soup.select('.news-item')
for news in news_list:
title = news.select('h2')[0].text.strip()
link = news.select('a')[0]['href']
news_response = requests.get(link)
news_response.encoding = 'utf-8'
news_soup = BeautifulSoup(news_response.text, 'lxml')
content = news_soup.select('#article')[0].text.strip()
print('标题:', title)
print('正文:', content)
```
这段代码会爬取新浪新闻中国频道的新闻标题和正文,并打印出来。你可以根据需要修改URL,或者将代码封装成函数以便在实际应用中使用。