爬取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')