beautifulsoup爬虫快速入门二—爬取鸡尾酒论坛数据实例

# -*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup
from lxml import html
import xml
import xlrd
import xlwt
import os
from xlutils.copy import copy

typeList = ['Gin', 'Rum', 'Vodka', 'Tequila', 'Whisky', 'Brandy']

drinkurl = 'http://www.drink8.cn/forum-Gin-1.html'
drink = requests.get(drinkurl)
bdrink = BeautifulSoup(drink.content, 'lxml')


def changeUrl(typenum):
    index = 'http://www.drink8.cn/'
    numList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
    cUrl = []
    for n in numList:
        a = index + 'forum-' + typenum + '-' + str(n) + '.html'
        cUrl.append(a)
    return cUrl


class enterDetail():

    # 参数化URL地址
    def changeUrl(typenum):
        index = 'http://www.drink8.cn/'
        numList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        cUrl = []
        for n in numList:
            a = index + 'forum-' + typenum + '-' + str(n) + '.html'
            cUrl.append(a)
        print(cUrl)
        return cUrl


    # 写入表格
    def detailUrl(drinkurl):

        drink = requests.get(drinkurl)
        bdrink = BeautifulSoup(drink.content, 'lxml')
        # 判断是否有文件,没有则创建一个表
        if os.path.exists('indexUrL.xls'):
            openXls = xlrd.open_workbook('indexUrL.xls')
            newXls = copy(openXls)
            newsheet = newXls.get_sheet(0)
            sheet1 = openXls.sheet_by_index(0)  # 获取第一张表
            nrows = sheet1.nrows
        else:
        #创建表格
            newXls = xlwt.Workbook()
            newsheet = newXls.add_sheet('Gin-Url', cell_overwrite_ok=True)

        datasp = bdrink.find_all('h3', class_='xw0')
        a = 0

        for i, j in enumerate(datasp):
            a = a + 1
            print(j.find('a')['href'])
            # 判断变量是否存在,有三种方法:返回True/False
            # 'nrows' in dir()/locals.keys()/vars.keys()
            if 'nrows' in dir():
                newsheet.write(nrows + i, 0, j.find('a')['href'])
            else:
                newsheet.write(i, 0, j.find('a')['href'])

        newXls.save('indexUrL.xls')


if __name__ == '__main__':
    enterDetail.changeUrl(typeList[0])

    for l in range(0,len(typeList)):
        for i in enterDetail.changeUrl(typeList[l]):

            enterDetail.detailUrl(i)
    print('ok,生成爬取地址')
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值