GithubHelp home page GithubHelp logo

Comments (6)

will-che avatar will-che commented on June 12, 2024 1

我也发现了上述问题:最后拿到的数据all,pc,wise的数据会全部变成wise的数据,但是有个疑问,为啥放在第一个循环会变成这样~

因为传入的是字典,第二层循环 三次追加的formated_data都是指向上一级作用域中的formated_data,所以更改了之后都会变成最后一次的。如果formated_data放入第二层的话,就会生成三个不一样地址的数据,这时候再添加就没有问题了。

但是,第二层每次循环后,不是立刻更新了self.result,按理说已经把更新值写入了,虽然我后来也是把formated_data写到第二层循环,解决问题的

你更新的self.result写入的是上级作用域中formated_data指向的地址,三次都是同一个地址,所以改了对应地址内的数据,三次的数据就都变了

from spider-baiduindex.

will-che avatar will-che commented on June 12, 2024

问题已解决,从http://index.baidu.com/Interface/ptbk?uniqid=67dbcdcb9621ab994b881981118d3270
这个请求中拿到cookie,放入config.py中可以解决问题

发现代码中的新问题,最后拿到的数据all,pc,wise的数据会全部变成wise的数据,原因是format_data函数中,formated_data放在了第一层for循环中,将其放到第二层for循环里可以解决问题
for i in range(time_len):
for kind in self._all_kind:
formated_data = {
'date': cur_date.strftime('%Y-%m-%d')
}

from spider-baiduindex.

Khaydn avatar Khaydn commented on June 12, 2024

我也发现了上述问题:最后拿到的数据all,pc,wise的数据会全部变成wise的数据,但是有个疑问,为啥放在第一个循环会变成这样~

from spider-baiduindex.

will-che avatar will-che commented on June 12, 2024

我也发现了上述问题:最后拿到的数据all,pc,wise的数据会全部变成wise的数据,但是有个疑问,为啥放在第一个循环会变成这样~

因为传入的是字典,第二层循环 三次追加的formated_data都是指向上一级作用域中的formated_data,所以更改了之后都会变成最后一次的。如果formated_data放入第二层的话,就会生成三个不一样地址的数据,这时候再添加就没有问题了。

from spider-baiduindex.

Khaydn avatar Khaydn commented on June 12, 2024

我也发现了上述问题:最后拿到的数据all,pc,wise的数据会全部变成wise的数据,但是有个疑问,为啥放在第一个循环会变成这样~

因为传入的是字典,第二层循环 三次追加的formated_data都是指向上一级作用域中的formated_data,所以更改了之后都会变成最后一次的。如果formated_data放入第二层的话,就会生成三个不一样地址的数据,这时候再添加就没有问题了。

但是,第二层每次循环后,不是立刻更新了self.result,按理说已经把更新值写入了,虽然我后来也是把formated_data写到第二层循环,解决问题的

from spider-baiduindex.

longxiaofei avatar longxiaofei commented on June 12, 2024

我也发现了上述问题:最后拿到的数据all,pc,wise的数据会全部变成wise的数据,但是有个疑问,为啥放在第一个循环会变成这样~

这是python的一个引用问题,python中的赋值操作,相当于引用对方的内存地址,对于可变对象,自身改变,内存地址不变,所以所有引用这个内存地址的变量都会跟着改变。
可以将对象的建立放在第二层循环中,也可以对对象做一个浅复制。
代码已改。

from spider-baiduindex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.