鱼C论坛

 找回密码
 立即注册
查看: 1939|回复: 0

[技术交流] Python采集问题请教

[复制链接]
发表于 2017-12-2 18:31:21 | 显示全部楼层 |阅读模式
100鱼币
本帖最后由 mskyer 于 2017-12-2 18:33 编辑

我用wordpress搭建了一个电子书的分享网站http://kindleshare.cn,依靠python进行更新
主要是把网上下载的电子书放到空间里去,然后 用walk.py来爬取文件名和文件路径
walk.py内容如下:
  1. # -*- coding: utf-8 -*-

  2. import os

  3. count = 0

  4. path = raw_input("input path:")
  5. for root, dirs, files in os.walk(path):

  6.    
  7.   for file in files:
  8.     count += 1
  9.     print os.path.join(root, file)
  10.     with open('bookinfo.txt', 'a') as fp:fp.write(os.path.join(root, file) +'\r\n')
  11.     fp.close()
  12. print count
复制代码
  

然后根据gengxin.py来读取书籍信息:
  1. #-*-coding:utf-8-*-


  2. from function import *


  3. if __name__ == '__main__':

  4.   i = int(input("请输入要更新文章的数目:"))
  5.   while i > 0:
  6.     f = open("tmp_book.txt","r",encoding='UTF-8')
  7.    
  8.     books = f.readline()
  9.     url = books.replace('\r\n','').replace("/www/wwwroot/","http://kindleshare.cn/").replace('\n','')
  10.     book = books.replace('/n','')
  11.     (filepath,book_name) = os.path.split(book)
  12.     (file_name,book_detail) = os.path.splitext(book_name)
  13.     lines = f.readlines()        
  14.     f = open("tmp_book.txt","w",encoding='UTF-8')
  15.     for line in lines:
  16.         f.write(line)
  17.     print(book_name)
  18.     try:
  19.         dizhi = search(file_name)
  20.         (info , tagg) = get_single_book_data(dizhi)

  21.         tagg.append(book_detail.replace('.',''))
  22.    

  23.         info += "\n\n"+"<span style="font-size: 18pt";>"+"<a href=" + """+ str(url).replace('\n','')+ """  + ">点击下载</a></span>"
  24.         print(info)

  25.         for s in tagg:
  26.             print(s)
  27.         
  28.       
  29.         fabu(file_name,info,list(map(str,tagg)))
  30.         print ("++++++++++++++++++" + "电子书《" + book_name + "》" + "发布成功" + "++++++++++++++++++")
  31.     except Exception as e:
  32.         with open('Errors.txt', 'a') as fp:fp.write(book)
  33.         
  34.         print(e)
  35.     i = i - 1
复制代码


那么问题来了,我现在需要完善两个函数,一个是search(file_name),可以根据文件名来搜索对应电子书在豆瓣上的链接
第二个函数就是get_single_book_data(dizhi),它是需要根据第一个函数的结果来采集电子书的相关信息。
我自己写了一遍,但是效果不好,正确率不是很高,看有没有高手能帮一下忙哈

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 01:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表