py3+requests+bs4+xlwt,爬取自己博客园博客标题和链接,并写入excel

除了首页(首页有置顶博客),其余页每页10篇博客

所以,先从非首页入手。

爬取思路还是和之前的一遍博客写的一样,http://www.cnblogs.com/UncleYong/p/6892688.html

这里给出详细代码及注释:

import requests, xlwt
from bs4 import  BeautifulSoup

url_list = [] # 用于存放标题和url

# 获取源码
def get_content(url):
    html = requests.get(url).content
    return html

# 获取某页中的所有博客url
def get_url(html):
    soup = BeautifulSoup(html, 'lxml') # lxml是解析方式,第三方库
    blog_url_list = soup.find_all('div', class_='postTitle')
    for i in blog_url_list:
        url_list.append([i.find('a').text, i.find('a')['href']])

for page in range(1,10):
    url = 'http://www.cnblogs.com/UncleYong/default.html?page={}'.format(page)
    # print(url)
    get_url(get_content(url))

newTable = 'blog_list2.xls'
wb = xlwt.Workbook(encoding='utf-8')  # 打开一个对象
ws = wb.add_sheet('blog')  # 添加一个sheet
headData = ['博客标题', '链接']
# 写标题
for colnum in range(0, 2):
    ws.write(0, colnum, headData[colnum], xlwt.easyxf('font:bold on'))  # 第0行的第colnum列写入数据headDtata[colnum],就是表头,加粗
index = 1
lens = len(url_list)
# 写内容
for j in range(0, lens):
    ws.write(index, 0, url_list[j][0])
    ws.write(index, 1, url_list[j][1])
    index += 1  # 下一行
wb.save(newTable)  # 保存

结果:

总共50条数据

因为有置顶的文章,所以,上面的url是有重复的,比如:

[置顶]python多线程测试接口性能,http://www.cnblogs.com/UncleYong/p/7004415.html

python多线程测试接口性能,http://www.cnblogs.com/UncleYong/p/7004415.html

所以,可以先存入字典,每次判断url是否在keys()中,不在的话,增加字典的key和value

import requests, xlwt
from bs4 import  BeautifulSoup

url_dic = {} # 用于存放标题和url
li = [] # 用于存放有序的标题和url

# 获取源码
def get_content(url):
    html = requests.get(url).content
    return html

# 获取某页中的所有博客url
def get_url(html):

    soup = BeautifulSoup(html, 'lxml') # lxml是解析方式,第三方库

    blog_url_list = soup.find_all('div', class_='postTitle')
    for i in blog_url_list:
        if i.find('a')['href'] not in url_dic.values():
            url_dic[i.find('a').text]=i.find('a')['href']

for page in range(1,10):
    url = 'http://www.cnblogs.com/UncleYong/default.html?page={}'.format(page)
    # print(url)
    get_url(get_content(url))

for i,j in url_dic.items():
    # print(i, j)
    li.append((i,j))

newTable = 'blog_list4.xls'
wb = xlwt.Workbook(encoding='utf-8')  # 打开一个对象
ws = wb.add_sheet('blog')  # 添加一个sheet
headData = ['博客标题', '链接']
for colnum in range(0, 2):
    ws.write(0, colnum, headData[colnum], xlwt.easyxf('font:bold on'))  # 第0行的第colnum列写入数据headDtata[colnum],就是表头,加粗
index = 1
lens = len(li)
for j in range(0, lens):
    ws.write(index, 0, li[j][0])
    ws.write(index, 1, li[j][1])
    index += 1  # 下一行
wb.save(newTable)  # 保存

这样,就少了两条被置顶的数据,获取到的是加了置顶二字的数据。

但是,如果是要获取到没有置顶二字的数据,那就通过切片,每次获取到标题都做切片操作

# 有置顶二字的标题
print('[置顶]python多线程测试接口性能'.split('[置顶]')[1])
# 无置顶二字的标题
print('python多线程测试接口性能'.split('[置顶]')[0])

但是问题又来了,因为标题中可能也包含‘[置顶]’,

可以这样:

li = ['[置顶]python多线程测试[置顶]接口性能','python多线程测试[置顶]接口性能','python多线程测试接口性能']
for i in li:
    print(i.center(50,'*'))
    if i.count('[置顶]') > 0:
        if i.split('[置顶]')[0]=='':
            # print(i.split('[置顶]')[1])
            print(''.join(i.split('[置顶]')))
        else:
            print(i)
    else:
        print(i)

原文地址:https://www.cnblogs.com/uncleyong/p/7078388.html