Py学习  »  问与答

小白求助,关于豆瓣抓取信息的问题

AGGYZ • 7 年前 • 1464 次点击  

使用的是Python2.7.10的版本,我是一名没学几天的初学者,从一个视频里基本照搬学习豆瓣图书信息抓取的写法,但是一直显示出错,(好像是.?这里一直显示出错),求大神指点一下哪里有问题。 多谢多谢!

# -*- coding:utf-8 -*-
from Tkinter import *
from ScrolledText import ScrolledText
import urllib
import re
import time
import threading

def get(ID):
    varl.set('have gotten %s books' %ID)
    html = lib.urllib.urlopen('https://read.douban.com/ebooks/tag/%E5%8E%86%E5%8F%B2/?cat=book&sort=top&start=20').read()
    #print html
    reg = r'<span class="price-tag ">(.*?)</span>.*?''read.douban.com\'\)">(.*?)</a>'
    return re.findall(reg,html)

#a = get()

def write():
    ID = 0 #声明变量,书本递增展示在GUI上的数量
    a =[]
    s = 0 #书名
    while ID<=600:
        L = get(ID)
        ID+=20
        for i in L:
            s+=1
            a.append(float(i[0]))
            text.insert(END,'书名:%s      评分与阅读人数:%s\n'%(i[1],i[0]))

    text.insert(END,'-----------------------------------------------\n')
    text.insert(END,'该分类书本总数量:%s\n' %s)
    text.insert(END,'平均每本书本%。2f分' %(sum(a)/s))
    fn = open('read.txt','w')
    fn.write(text.get(1.0,END)).encode('gbk')
    fn.close()
    varl.set('all done')

def th():
    t1 = threading.Thread(target=write) 
    t1.start() #启动线程

root = Tk()
root.title('豆瓣历史图书')
text = ScrolledText(root,font=('微软雅黑',10))
text.grid()
button = Button(root,text='start',font=('微软雅黑',10),command = th)
button.grid()
varl = StringVar()#设置变量,tk绑定一个变量
label = Label(root,font=('微软雅黑',10),fg='blue',textvariable = varl)
label,grid()
varl.set('preparing...')
root.mainloop()#进入消息循环,发送命令
最后一次修改于 (2017-03-14 22:33)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1790
 
1464 次点击  
文章 [ 2 ]  |  最新文章 7 年前
筑梦者
Reply   •   1 楼
筑梦者    7 年前

根据我的经验,一般都是链接的网址出现错误或者获取的数据字典或者列表名字发生变化,导致程序运行失败的,建议你先从测试lib.urllib.urlopen()开始,打开豆瓣的网页源码,浏览看获取的是否想要的,一步一步实现,不然全部代码一次性写上你不会调试

AGGYZ
Reply   •   2 楼
AGGYZ    7 年前

def get(ID): varl.set('have gotten %s books' %ID) html = lib.urllib.urlopen('https://read.douban.com/ebooks/tag/%E5%8E%86%E5%8F%B2/?cat=book&sort=top&start=20').read() #print html reg = r'<span class="price-tag ">(.?)</span>.*?''read.douban.com\')">(.?)</a>' return re.findall(reg,html)

这一部分