python爬取http://www.people.com.cn/人民网
时间: 2024-10-03 08:01:13 浏览: 145
在Python中,你可以使用requests库来发送HTTP请求,并结合BeautifulSoup或者lxml库解析HTML内容,以便爬取网页数据。下面是一个简单的例子,展示如何爬取"人民网"首页的部分信息:
```python
import requests
from bs4 import BeautifulSoup
url = "http://www.people.com.cn/"
response = requests.get(url)
# 检查请求是否成功
if response.status_code == 200:
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 找到特定的元素,这里以新闻标题为例
title_elements = soup.find_all('h2', class_='title') # 假设标题有特定CSS类
for title in title_elements:
print(title.text) # 打印每个标题的内容
else:
print("请求失败,状态码:", response.status_code)
相关问题
1. 爬取人民网首页http://www.people.com.cn/国际 · 军事 模块下的新闻标题和出现的图片。
好的,以下是针对该需求的Python爬虫代码实现:
```python
import requests
from bs4 import BeautifulSoup
url = "http://www.people.com.cn/"
response = requests.get(url)
response.encoding = 'gbk'
soup = BeautifulSoup(response.text, "html.parser")
# 获取“国际·军事”模块下的新闻链接
module = soup.find("div", class_="ej_channel")
news_links = module.find_all("a")
for link in news_links:
if link.get("href") and "world" in link.get("href") and "people" in link.get("href"):
news_url = link.get("href")
break
# 进入新闻页面并获取新闻标题和图片
response = requests.get(news_url)
response.encoding = 'gbk'
soup = BeautifulSoup(response.text, "html.parser")
title = soup.find("h1").text
print("新闻标题:", title)
images = soup.find_all("img")
for image in images:
if image.get("src") and "http" in image.get("src"):
print("新闻图片:", image.get("src"))
```
解释一下代码实现的思路:首先,我们访问人民网首页并获取页面内容,然后使用BeautifulSoup库解析页面。通过查看页面源代码,我们可以发现“国际·军事”模块下的新闻链接是通过a标签的href属性指定的。找到该模块的div标签,遍历其中的所有a标签,通过判断href属性中是否包含“world”和“people”字符串来确定是否是我们需要的新闻链接。最后,进入新闻页面,通过查看页面源代码,我们可以发现新闻标题是通过h1标签包含的文本来指定的,而新闻图片则是通过img标签的src属性指定的。因此,我们可以使用BeautifulSoup库的find和find_all方法来获取这些信息,最终输出到控制台。
scrapy爬取人民网http://search.people.cn/s?keyword=%E7%88%AC%E8%99%AB&st=0&_=1733737096163里各条新闻的标题,作者,发布时间
要使用Scrapy爬取人民网(http://search.people.cn/s?keyword=%E7%88%AC%E8%99%AB&st=0&_=1733737096163)里各条新闻的标题、作者和发布时间,可以按照以下步骤进行:
1. **安装Scrapy**:
首先,确保你的环境中已经安装了Scrapy。如果没有安装,可以使用以下命令进行安装:
```bash
pip install scrapy
```
2. **创建Scrapy项目**:
在命令行中创建一个新的Scrapy项目:
```bash
scrapy startproject renminwang
```
3. **编写爬虫**:
在`renminwang/spiders`目录下创建一个新的爬虫文件,例如`news_spider.py`:
```python
import scrapy
from scrapy.http import Request
class NewsSpider(scrapy.Spider):
name = "news"
start_urls = [
'http://search.people.cn/s?keyword=%E7%88%AC%E8%99%AB&st=0&_=1733737096163'
]
def parse(self, response):
# 提取新闻列表
news_list = response.css('div.list-item')
for news in news_list:
title = news.css('h3 a::text').get()
author = news.css('p.author::text').get()
publish_time = news.css('p.time::text').get()
yield {
'title': title,
'author': author,
'publish_time': publish_time
}
# 提取下一页链接并继续爬取
next_page = response.css('a.next::attr(href)').get()
if next_page:
yield response.follow(next_page, self.parse)
```
4. **运行爬虫**:
在项目根目录下运行以下命令启动爬虫:
```bash
scrapy crawl news -o news.json
```
这将把爬取到的数据保存到`news.json`文件中。
5. **处理数据**:
爬取到的数据会以JSON格式保存,你可以在后续的处理中使用这些数据。
阅读全文
相关推荐
















