python-2.x

Web-scraping JavaScript page with Python

佐手、 提交于 2019-12-11 03:09:47
问题 I'm trying to develop a simple web scraper. I want to extract text without the HTML code. In fact, I achieve this goal, but I have seen that in some pages where JavaScript is loaded I didn't obtain good results. For example, if some JavaScript code adds some text, I can't see it, because when I call response = urllib2.urlopen(request) I get the original text without the added one (because JavaScript is executed in the client). So, I'm looking for some ideas to solve this problem. 回答1: EDIT 30

Problems with python 2.4 and 2.4.4 in struct.unpack and win/lin

戏子无情 提交于 2019-12-11 02:58:39
问题 i'm coding on a Linux debian machine with python 2.4. My neighbour with Windows XP and python 2.4.4 He can run this code: w1, w2, w3 = unpack("LLL", pack("LLHH", localtime, ipddr, counter, aid)) But when i try this code i become this error: w1, w2, w3 = unpack("LLL", pack("LLHH", localtime, ipddr, counter, aid)) struct.error: unpack str size does not match format Can it be the Version of python or maybe the system? 回答1: Use the I format code instead of L . On your Linux machine, L isn't twice

Length of binary data in python

为君一笑 提交于 2019-12-11 01:39:15
问题 I am using Python. I am trying to determine the correct length of bytes in a binary set of data. If I assign a variable the binary data... x = "aabb".decode("hex") is that the same as x = b'aabb' And if so, how do you get how many bytes that is? (It should be 2 bytes) When I try: len(x) I get 4 instead of 2 though... I am worried that x is turned into a string or something else I don't understand because the data types are so fluid in Python... 回答1: The length of binary data is just the len ,

Detach from running process

人盡茶涼 提交于 2019-12-10 23:12:34
问题 I am facing a funny situation: I'm developing a flashlight plugin to trigger an alarm at some point in the future. The problem is, flashlight kills the script after 30 seconds. So I can neither wait long enough to activate the alarm nor play the alarm indefinitely until the user cancels it. So, I am looking to find a way for the script to "detach" itself from (or otherwise prevent itself from being killed by) its parent process. Could this be accomplished by daemonising itself? 回答1: I believe

In python 2.x should I call object.__del__?

牧云@^-^@ 提交于 2019-12-10 21:29:31
问题 In Python 3.x all classes are subclasses of object . In 2.x you have to explicitly state class MyClass(object) . And, as I'm trying to write as much 3.x compatible code as possible, I'm subclassing object . In my program, I'm using the __del__ method, and I wanted to know if I should be calling object.__del__(self) , or if that's magically taken care of? Thanks, Wayne EDIT: It appears there is some confusion what I mean - in the documents it states: If a base class has a __del__() method, the

getting underlying OLE object identity for win32com automation objects

风流意气都作罢 提交于 2019-12-10 21:23:45
问题 Most built-in Python data types and libraries make a point of returning the same object ( a is b , not just a==b ) even if you ask for it in different ways. A very simple example: list = [ "foo", "bar", {"name": [1,2,3]} ] a = list[-1]["name"] b = list[2].values()[0] print (a is b) # True! However, this doesn't seem to be the case for many kinds of non-scalar objects returned by win32com automation. The following code connects to sas-jmp automation and then gets two handles to the same data

How to open a file with a known encoding on both Python2 and 3?

心已入冬 提交于 2019-12-10 20:32:50
问题 When opening a file known to be utf-8 on a script that needs to be Py2 & 3 compatible. Is there a nicer way to do it than this: if sys.version_info < (3, 0): long_description = open('README').read() else: long_description = open('README', encoding='utf-8').read() Calling open('README').read() on Python3.x causes encoding error for systems that default to ascii . 回答1: You could use the io.open function, which is the built-in open() in Python 3. from io import open long_description = open(

Iterating and Updating the list in python [duplicate]

我的未来我决定 提交于 2019-12-10 15:43:25
问题 This question already has answers here : What is the meaning of list[:] in this code? [duplicate] (4 answers) Closed 2 years ago . I am not able to understand why the following code goes in indefinite loop(when i am not using the copy list) list = ["Mohit","kumar","sffsfshfsd"] for w in list: if(len(w)) > 5: list.insert(0,w) print("inside loop") print(list) The Above code prints inside loop indefinitely. Now if in place of the list, i use a copy list like below works fine. list = ["mohit",

How to create a tkinter toggle button?

二次信任 提交于 2019-12-10 13:43:23
问题 I've been working on a text editor using Tkinter in Python 2.7. A feature that I'm trying to implement is the Night Mode, where the user can toggle between a black background and a light one, that switches from light to dark with a click of the toggle button. from Tkinter import * from tkSimpleDialog import askstring from tkFileDialog import asksaveasfilename from tkFileDialog import askopenfilename from tkMessageBox import askokcancel Window = Tk() Window.title("TekstEDIT") index = 0 class

Custom class ordering: no error thrown, what is Python testing for?

只愿长相守 提交于 2019-12-10 12:51:36
问题 Without specifying the equality comparison properties of objects, Python is still doing something when using > and < . What is Python actually comparing these objects by if you don't specify __gt__ or __lt__ ? I would expect an unsupported operand error here, as you get when trying to add two objects together without defing __add__ . In [1]: class MyObject(object): ...: pass ...: In [2]: class YourObject(object): ...: pass ...: In [3]: me = MyObject() In [4]: you = YourObject() In [5]: me >