python爬虫--小白爬取csdn页面题目与链接

爬取csdn页面题目与链接

前言

随着人工智能的不断发展,爬虫这门技术越来越重要…哈哈哈,太过官方。新手小白,过程较曲折,代码较不专业,欢迎批评与指教!
进入正题:本文主要爬取csdn博客某专栏下的题目与链接,并保存到excel文件中,

一、爬取页面

二、代码

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import pandas as pd
import numpy as np
import openpyxl


html=urlopen('https://ai.csdn.net/?spm=1000.2115.3001.4125').read().decode('utf-8')
all_res1=[]
all_res2=[]
soup=BeautifulSoup(html,features='html.parser')
al=soup.find_all('div',{'class':'title'})
for one in al:
    res=one.find('a')  
    res=str(res).split('\n')
    res[0]=res[0].replace('<a ','')
    res[0]=res[0].replace(' target="_blank">','')
    res[1]=res[1].replace('</a>','')
    res[1]=res[1].strip()
    all_res1.append(res[0])
    all_res2.append(res[1])

dict={'题目':all_res2,'链接':all_res1}
df=pd.DataFrame(dict)
df.to_excel('E:/output/output.xlsx')

爬取结果

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值