用jupyter爬取微博热搜的代码
时间: 2023-08-04 10:31:57 浏览: 50
好的,以下是一个在jupyter中爬取微博热搜的代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://s.weibo.com/top/summary?cate=realtimehot'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
hot_list = soup.select('.list_a > tr')
for hot in hot_list:
rank = hot.select_one('.td-01.ranktop').text.strip()
title = hot.select_one('.td-02 > a').text.strip()
heat = hot.select_one('.td-02 > span').text.strip()
print(rank, title, heat)
```
将代码复制粘贴到jupyter中的代码单元格中,执行即可。
相关问题
用jupyter爬取微博关键词
当使用Jupyter进行微博关键词爬取时,你可以按照以下步骤进行操作:
1. 安装必要的库:首先,你需要安装Python的requests、beautifulsoup4和pandas库。你可以使用以下命令来安装它们:
```
pip install requests beautifulsoup4 pandas
```
2. 导入所需的库:在Jupyter Notebook中,你需要导入这些库以便使用它们。使用以下代码导入它们:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
```
3. 发送请求并解析页面:使用requests库发送HTTP请求,并使用BeautifulSoup库解析返回的HTML页面。以下是一个示例代码:
```python
# 发送请求
url = 'https://s.weibo.com/weibo?q=关键词'
response = requests.get(url)
# 解析页面
soup = BeautifulSoup(response.text, 'html.parser')
```
请将"关键词"替换为你要搜索的实际关键词。
4. 提取所需信息:根据页面的HTML结构,使用BeautifulSoup库提取你需要的信息。例如,如果你想提取微博的文本内容和发布时间,可以使用以下代码:
```python
# 提取微博内容和发布时间
weibo_list = soup.find_all('div', class_='content')
for weibo in weibo_list:
text = weibo.find('p').text.strip()
time = weibo.find('p', class_='from').find('a').text.strip()
print(text, time)
```
5. 存储数据:如果你想将提取的数据保存到文件中,可以使用pandas库将数据转换为DataFrame,并将其保存为CSV文件。以下是一个示例代码:
```python
# 创建DataFrame并保存为CSV文件
data = {'Text': [], 'Time': []}
for weibo in weibo_list:
text = weibo.find('p').text.strip()
time = weibo.find('p', class_='from').find('a').text.strip()
data['Text'].append(text)
data['Time'].append(time)
df = pd.DataFrame(data)
df.to_csv('weibo_data.csv', index=False)
```
请确保你已经创建了一个名为"weibo_data.csv"的文件。
使用jupyter爬取晋江文学网
首先,你需要安装以下几个库:requests、beautifulsoup4 和 pandas。在Jupyter Notebook中打开一个新的Python文件,然后按照以下步骤开始爬取晋江文学网上的小说数据:
1. 导入所需的库:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
```
2. 确定要爬取的小说的URL:
```python
url = 'https://www.jjwxc.net/onebook.php?novelid=123456'
```
请将"123456"替换为你想要爬取的小说的实际ID号。
3. 使用requests库获取HTML源代码:
```python
response = requests.get(url)
html = response.content
```
4. 使用BeautifulSoup库解析HTML源代码:
```python
soup = BeautifulSoup(html, 'html.parser')
```
5. 找到包含小说章节列表的HTML元素:
```python
chapter_list = soup.find('div', {'class': 'booklast'})
```
6. 找到所有小说章节的链接和标题:
```python
chapter_links = chapter_list.find_all('a')
chapter_titles = [chapter.text for chapter in chapter_links]
chapter_urls = ['https://www.jjwxc.net/' + chapter.get('href') for chapter in chapter_links]
```
7. 遍历所有章节链接,获取每章节的HTML源代码:
```python
chapter_html = []
for chapter_url in chapter_urls:
chapter_response = requests.get(chapter_url)
chapter_html.append(chapter_response.content)
```
8. 使用BeautifulSoup库解析每章节的HTML源代码,并提取章节内容:
```python
chapter_content = []
for chapter in chapter_html:
chapter_soup = BeautifulSoup(chapter, 'html.parser')
chapter_content.append(chapter_soup.find('div', {'class': 'noveltext'}).text)
```
9. 将小说章节的标题和内容存储到一个Pandas DataFrame中:
```python
novel_df = pd.DataFrame({'title': chapter_titles, 'content': chapter_content})
```
现在,你已经成功地爬取了晋江文学网上的小说数据,并将其存储在一个DataFrame中。可以使用Pandas的导出功能将数据导出为CSV或Excel文件,以便进一步分析。