python-2.7

Clicking links with Python BeautifulSoup

六眼飞鱼酱① 提交于 2021-01-27 17:47:44
问题 So I'm new to Python (I come from a PHP/JavaScript background), but I just wanted to write a quick script that crawled a website and all children pages to find all a tags with href attributes, count how many there are and then click the link. I can count all of the links, but I can't figure out how to "click" the links and then return the response codes. from bs4 import BeautifulSoup import urllib2 import re def getLinks(url): html_page = urllib2.urlopen(url) soup = BeautifulSoup(html_page,

Parsing HTML page containing & using Python

て烟熏妆下的殇ゞ 提交于 2021-01-27 16:13:12
问题 I am trying to parse HTML page in python using urllib2 and ElementTree and I am facing trouble parsing the HTML. Webpage contains "&" within quoted string but ElementTree throws parseError for lines containing & Script: import urllib2 url = 'http://eciresults.nic.in/ConstituencywiseU011.htm' req = urllib2.Request(url, headers={'Content-type': 'text/xml'}) r = urllib2.urlopen(req).read() import xml.etree.ElementTree as ET htmlpage=ET.fromstring(r) This throws following error in Python 2.7

Parsing HTML page containing & using Python

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-27 16:10:47
问题 I am trying to parse HTML page in python using urllib2 and ElementTree and I am facing trouble parsing the HTML. Webpage contains "&" within quoted string but ElementTree throws parseError for lines containing & Script: import urllib2 url = 'http://eciresults.nic.in/ConstituencywiseU011.htm' req = urllib2.Request(url, headers={'Content-type': 'text/xml'}) r = urllib2.urlopen(req).read() import xml.etree.ElementTree as ET htmlpage=ET.fromstring(r) This throws following error in Python 2.7

Python: replacing multiple words in a text file from a dictionary

馋奶兔 提交于 2021-01-27 14:40:25
问题 I am having trouble figuring out where I'm going wrong. So I need to randomly replace words and re-write them to the text file, until it no longer makes sense to anyone else. I chose some words just to test it, and have written the following code which is not currently working: # A program to read a file and replace words until it is no longer understandable word_replacement = {'Python':'Silly Snake', 'programming':'snake charming', 'system':'table', 'systems':'tables', 'language':'spell',

Numpy: Replace every n element in the first half of an array

╄→гoц情女王★ 提交于 2021-01-27 14:38:08
问题 If I have a numpy array and want to replace every nth element to 0 in the first half of the array( no change in the second half), how can I do this efficiently? Now my code is not efficient enough: for i in xrange(1,half,n): s[i] = 0 回答1: Just use a[:a.size//2:n] = 0 . e.g.: a = np.ones(10) a[:a.size//2:2] = 0 a array([ 0., 1., 0., 1., 0., 1., 1., 1., 1., 1.]) Another example: a = np.ones(20) n = 3 a[:a.size//2:n] = 0 a array([ 0., 1., 1., 0., 1., 1., 0., 1., 1., 0., 1., 1., 1., 1., 1., 1., 1

How to run an autohotkey script in Python (2.7)

柔情痞子 提交于 2021-01-27 14:07:53
问题 I'm trying to run an autohotkey (ahk) script in Python 2.7 but nothing seems to work. All online sources I've found are either outdated or overly complicated. Has anyone found a way of doing this? I just want to run a couple of simple scripts that activates windows and opens applications. E.g: IfWinExist, Command Prompt - python ... WinActivate Update: I've tried downloading pyahk: ahk.start() # Ititializes a new script thread ahk.ready() # Waits until status is True ahk.execute(mw._['cwd']+

Getting 'Missing required field: member' when trying to add a member to a google group via API

江枫思渺然 提交于 2021-01-27 13:54:06
问题 Trying to use Google admin directory API in order to read members of a google group (organization) - it works fine. When I try to add a member I get: { errors: [ { domain: 'global', reason: 'required', message: 'Missing required field: member' } ], code: 400, message: 'Missing required field: member' } I've googled the error and found questions like this, this and a few other unhelpful results. I checked and it's definitely not a missing scope nor permissions. #!/usr/bin/python import

ValueError: too many values to unpack python 2.7

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-27 13:16:43
问题 So I am trying to compile the following code but it's showing me the error on cv2.findContours. Though, I am using Python 2.7 version. Any reason as to why the error: too many values to unpack python 2.7 is coming? import cv2 import numpy as np import time #Open Camera object cap = cv2.VideoCapture(0) #Decrease frame size cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1000) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600) def nothing(x): pass # Function to find angle between two vectors def Angle(v1,v2): dot = np

Importing external package once in my module without it being added to the namespace

不羁岁月 提交于 2021-01-27 13:14:38
问题 I apologize for not being able to phrase my question more easily. I am writing a large package that makes extensive use of pandas in almost every function. My first instinct, naturally, was to create an __init__.py as import pandas # then import my own submodules and other things And then, every time I use pandas in a function, call it from the submodules as from . import pandas as pd or from .. import pandas , or something like that. However, if I do this, when I load my package, pandas

Select Javascript created element in Selenium Python

别来无恙 提交于 2021-01-27 13:05:47
问题 I have the following element in a web page. <button type="submit" class="zsg-button_primary contact-submit-button track-ga-event" data-ga-category="contact" data-ga-action="email" data-ga-label="rentalbuilding" data-ga-event-content="false" data-ga-event-details="" id="yui_3_18_1_2_1482045459111_1278"> <span class="zsg-loading-spinner hide"></span> <span class="button-text" id="yui_3_18_1_2_1482045459111_1277">Contact Property Manager</span> </button> I can find this element with