python 爬虫 练习 爬小说排行榜跳2个网页

这个只爬了一个排行榜其他的忘了写。。。但都一样可以看我写的另一个那个注释比较全,都大同小异




# -*- coding:utf-8 -*-
import re
import sys
import os
from time import sleep

from bs4 import BeautifulSoup
import requests
reload(sys)
sys.setdefaultencoding('utf-8')
headers={'User-Agent':'自己写'}
def port(url,localPath):
    req = requests.get(url, headers=headers).text
    soup = BeautifulSoup(req, 'html.parser')
    nums = soup.find_all(class_='nums')
    pp= nums[0].get_text().strip()
    book_name = soup.find_all(class_='book-name')
    pp2 = "书名: "+book_name[0].get_text().strip()
    au_name = soup.find_all(class_='au-name')
    pp3 ='作者: '+au_name[0].get_text().strip()
    yp = soup.find_all(class_='fr rank-r')
    pp4 ='月票排行: '+ yp[0].get_text().replace('\n',"").strip()
    print pp4
    if not os.path.exists(localPath):  # 新建文件夹
            os.mkdir(localPath)
    fn = open(localPath+"tt.txt",'a+')
    ff=pp2+" "+pp3+" "+pp+" "+pp4+"\n"
    print "正在写入"+ff
    fn.write(ff)
    fn.close()
def xs():
    url='http://book.zongheng.com/rank.html'
    req=requests.get(url,headers=headers).text
    soup=BeautifulSoup(req,'html.parser')
    list=soup.find_all(class_='rank_i_bname fl')//找到class是这个玩意的就是点进去的第2个网页
    i=0
    for i in range(0,len(list)):
        url1 = re.findall(r'href="(.*?/book/.*?)"',str(list[i]))#获取href中有book的链接
        path='E:/Desktop/img/'
        uipath = unicode(path, 'utf-8')
        port(str(url1).strip("['").strip("']"),uipath)
if __name__ == '__main__':
    xs():
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值