python爬虫小红书
时间: 2023-07-14 17:58:01 浏览: 64
爬取小红书的数据一般可以使用 Python 中的 requests 和 BeautifulSoup 库来实现。以下是一个简单的爬取小红书的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.xiaohongshu.com/discovery/item/5ffeb8b0000000000100e8d7"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# 获取标题
title = soup.find('h1', class_='title').text
print(title)
# 获取正文
content = soup.find('div', class_='content').text
print(content)
# 获取评论
comments = soup.find_all('div', class_='comment')
for comment in comments:
print(comment.text)
```
需要注意的是,爬取小红书的数据是有风险的,需要遵守相关法律法规和小红书的规定,不要进行非法爬取和数据滥用。
相关问题
python爬虫小红书代码
当然可以!以下是一个简单的Python爬虫代码示例,用于爬取小红书网站的数据:
```python
import requests
from bs4 import BeautifulSoup
# 发送HTTP请求获取网页内容
url = "https://www.xiaohongshu.com/"
response = requests.get(url)
html_content = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, "html.parser")
# 提取需要的数据
data_list = []
items = soup.find_all("div", class_="item")
for item in items:
title = item.find("h3").text.strip()
author = item.find("span", class_="author").text.strip()
data_list.append({"title": title, "author": author})
# 打印提取的数据
for data in data_list:
print("标题:", data["title"])
print("作者:", data["author"])
print("--")
```
这段代码使用了`requests`库发送HTTP请求获取小红书网站的内容,并使用`BeautifulSoup`库解析网页内容。然后,通过查找特定的HTML元素,提取了标题和作者信息,并将其存储在一个列表中。最后,遍历列表并打印提取的数据。
python 爬虫小红书视频
可以使用Python中的Selenium和BeautifulSoup库来爬取小红书视频。首先,使用Selenium打开小红书页面,然后使用BeautifulSoup分析HTML,找到视频的URL,最后下载视频即可。
以下是一个简单的示例代码:
```python
from selenium import webdriver
from bs4 import BeautifulSoup
import requests
# 打开小红书页面
driver = webdriver.Chrome()
driver.get("https://www.xiaohongshu.com/")
# 在搜索框中输入关键词并搜索
search_box = driver.find_element_by_name("q")
search_box.send_keys("美妆")
search_box.submit()
# 获取搜索结果页面的HTML
html = driver.page_source
soup = BeautifulSoup(html, "html.parser")
# 找到视频的URL
video_url = soup.find("video")["src"]
# 下载视频
response = requests.get(video_url)
with open("video.mp4", "wb") as f:
f.write(response.content)
# 关闭浏览器
driver.quit()
```