python爬取csdn上的包含整人关键词的阅读量并且存入表格里
时间: 2023-02-08 22:42:05 浏览: 54
首先需要安装第三方库beautifulsoup4和pandas。然后可以使用python中的requests库获取网页源码,再使用beautifulsoup4解析网页源码,找到包含“整人”关键词的文章的阅读量。最后使用pandas库将阅读量存入表格中。
具体代码如下:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://blog.csdn.net/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
data = []
for item in soup.find_all('span', class_='read-count'):
if '整人' in item.text:
data.append(item.text)
df = pd.DataFrame(data, columns=['阅读量'])
df.to_excel('阅读量.xlsx', index=False)
```
上面代码中的url可以替换为需要爬取的网页地址,可以在代码中通过改变find_all()函数的参数来爬取不同的信息。
注意爬虫技术需要遵循网站的相关规则,否则可能会被封禁,请谨慎使用。
相关问题
python爬取csdn上的包含整人关键词的阅读量并且存入表格里。完整代码
### 回答1:
下面是一个爬取CSDN博客中包含"整人"关键词文章阅读量并存入表格的示例代码:
```python
import requests
import pandas as pd
from bs4 import BeautifulSoup
# 创建一个空的DataFrame存储爬取的数据
data = pd.DataFrame(columns=['标题','阅读量'])
# 遍历每一页
for i in range(1, 5):
url = 'https://blog.csdn.net/search/articleList?type=blog&keyword=%E6%95%B4%E4%BA%BA&page=' + str(i)
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')
# 查找每篇文章的标题和阅读量
for article in soup.find_all(class_='search-list-item'):
title = article.find('a').text
views = article.find(class_='search-list-item-views').text
# 将数据添加到DataFrame中
data = data.append({'标题':title,'阅读量':views},ignore_index=True)
# 保存爬取的数据到CSV文件中
data.to_csv('csdn_data.csv',index=False)
```
上面代码使用了requests库来发送请求,使用BeautifulSoup库来解析HTML,使用pandas库来存储数据。这个代码会爬取前4页的数据,如果需要爬取更多,可以更改for循环的终止条件。
需要注意的是,爬取网页数据时需要遵守网站的相关规则,并且需要负责自己的行为,对于不遵守相关规则的行为自负相应的后果。
### 回答2:
下面是一个使用Python爬取CSDN上包含整人关键词的博文阅读量,并将其存入表格的示例代码。代码使用了BeautifulSoup库来解析网页内容,以及pandas库来创建和管理表格。
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
# 创建空的表格
df = pd.DataFrame(columns=['标题', '阅读量'])
# 设定关键词
keyword = "整人"
# 爬取CSDN上的博文列表页面
url = "https://so.csdn.net/so/search/s.do?q=" + keyword
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取爬取结果中的博文标题和阅读量
titles = soup.select('.limit_width')
read_counts = soup.select('.read-num')
# 遍历博文列表,将标题和阅读量存入表格
for title, read_count in zip(titles, read_counts):
title_text = title.get_text(strip=True)
read_count_text = read_count.get_text(strip=True)
df = df.append({'标题': title_text, '阅读量': read_count_text}, ignore_index=True)
# 将表格保存为CSV文件
df.to_csv('csdn阅读量表格.csv', index=False)
```
上述代码首先导入需要的库,然后使用requests发送一个GET请求,获取CSDN上包含关键词的搜索结果页面。接下来,使用BeautifulSoup解析网页内容,通过CSS选择器选择博文的标题和阅读量元素。然后,遍历这些元素,将标题和阅读量存入表格中。
最后,使用pandas将表格保存为CSV文件。
### 回答3:
使用Python爬取CSDN上包含整人关键词的博文阅读量,并将结果存入表格的完整代码如下:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
def parse_page(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'lxml')
articles = soup.find_all('div', class_='article-item-box csdn-tracking-statistics')
data = []
for article in articles:
title = article.find('h4').text.strip()
read_count = article.find('span', class_='read-count').text.strip()
if '整人' in title:
data.append({
'标题': title,
'阅读量': read_count
})
return data
def save_to_excel(data):
df = pd.DataFrame(data)
df.to_excel('csdn_read_count.xlsx', index=False)
def main():
url = 'https://blog.csdn.net/'
page = 1
data = []
while True:
page_url = url + '?&page=' + str(page)
page_data = parse_page(page_url)
if len(page_data) == 0:
break
data.extend(page_data)
page += 1
save_to_excel(data)
if __name__ == '__main__':
main()
```
该代码首先使用`requests`库发送请求,使用`BeautifulSoup`库解析HTML页面。在`parse_page`函数中,我们定义了爬取和解析页面的过程。我们使用`css`选择器定位到博文列表中的每篇博文,并提取标题和阅读量信息。如果标题中包含“整人”关键词,我们将其标题和阅读量添加到`data`列表中。
`save_to_excel`函数用于将爬取的数据保存到Excel表格中,使用`pandas`库创建DataFrame对象,并通过调用`to_excel`方法将数据保存到`csdn_read_count.xlsx`文件中。
在`main`函数中,我们从第一页开始依次爬取博文,直到没有更多博文后结束循环。最后,调用`save_to_excel`函数将数据保存到表格中。
执行`main`函数即可开始爬取,并将结果保存到表格中。
python爬取csdn上的包含整人关键词的阅读量
### 回答1:
可以使用Python的爬虫库如Scrapy或Requests+BeautifulSoup来爬取CSDN上包含整人关键词的文章的阅读量。首先需要找到文章的URL,然后解析页面内容并提取阅读量信息。
例如:
```
import requests
from bs4 import BeautifulSoup
url = 'https://blog.csdn.net/weixin_43786778/article/details/114512345'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
read_num = soup.find('span', class_='read-count').text
print(read_num)
```
上面的代码中使用了requests库发送请求获取文章页面的html,使用beautifulsoup来解析html。然后使用find方法查找阅读量标签并提取阅读量信息。
如果要爬取包含整人关键词的文章的阅读量,可以先使用爬虫爬取CSDN上所有文章的链接,然后遍历每篇文章的链接,判断文章标题或内容中是否包含整人关键词,如果包含就爬取阅读量。
### 回答2:
在Python中,我们可以使用第三方库`requests`和`BeautifulSoup`来爬取CSDN上包含整人关键词的文章阅读量。
首先,我们需要安装这两个库。可以使用以下命令进行安装:
```
pip install requests
pip install BeautifulSoup4
```
然后,我们可以编写以下代码来实现爬取功能:
```python
import requests
from bs4 import BeautifulSoup
def get_reading_count(keyword):
url = f'https://so.csdn.net/so/search/s.do?q={keyword}&t=&u='
# 发送GET请求,获取网页内容
response = requests.get(url)
html = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html, 'html.parser')
# 找到包含阅读量信息的元素
reading_count_elems = soup.find_all('span', class_='read-num')
# 提取阅读量
reading_count = 0
for elem in reading_count_elems:
count_str = elem.text.strip().replace('阅读数:', '')
count = int(count_str)
reading_count += count
return reading_count
if __name__ == '__main__':
keyword = '整人'
reading_count = get_reading_count(keyword)
print(f'包含关键词"{keyword}"的文章总阅读量为:{reading_count}')
```
以上代码首先构造了一个带有关键词参数的CSDN搜索URL,然后发送GET请求,获取到搜索结果页面的HTML内容。接着,使用BeautifulSoup解析HTML,找到包含阅读量信息的元素,并提取出阅读量并累加。最后,返回总阅读量。
这样,就可以通过`get_reading_count`函数来爬取CSDN上包含整人关键词的文章阅读量了。
### 回答3:
要实现这个功能,我们可以使用Python的爬虫库(如BeautifulSoup)来爬取CSDN网站上包含特定关键词的文章,并获取这些文章的阅读量。
首先,我们需要安装相应的库,如requests和BeautifulSoup。可以使用以下命令进行安装:
```
pip install requests
pip install beautifulsoup4
```
接下来,我们可以使用requests库发送HTTP请求获取网页内容,并使用BeautifulSoup库解析HTML。
以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
def get_reading_count(keyword):
url = 'https://so.csdn.net/so/search/s.do?q={0}&t=&u='.format(keyword)
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 找到搜索结果列表
results = soup.find_all('dl', class_='search-list J_search')
for result in results:
# 找到文章标题
title = result.find('a', class_='limit_width').text
# 找到阅读量
reading_count = result.find('span', class_='read-num').text
print('标题:', title)
print('阅读量:', reading_count)
keyword = '整人' # 设置要搜索的关键词
get_reading_count(keyword)
```
上面的代码会输出包含关键词“整人”的文章标题和阅读量。
需要注意的是,为了避免被网站反爬虫机制拦截,我们在headers中设置了User-Agent,可以根据自己的情况进行修改。
另外,这只是一个简单的示例,实际应用中还需要考虑异常处理、分页爬取等问题。
相关推荐
![](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)
![](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)