urllib

Python urllib,urllib2 fill form

ⅰ亾dé卋堺 提交于 2019-12-05 19:25:33
I want to fill a HTML form with urllib2 and urllib . import urllib import urllib2 url = 'site.com/registration.php' values = {'password' : 'password', 'username': 'username' } data = urllib.urlencode(values) req = urllib2.Request(url, data) response = urllib2.urlopen(req) the_page = response.read() But on the end of the form is a button(input type='submit') . If you don't click the button you can't send the data what you wrote in the input(type text) How can I click the button with urllib and urllib2 ? This is really more of something you would do with Selenium or similar. selenium.webdriver

Fetch first n bytes from the URL

强颜欢笑 提交于 2019-12-05 18:51:46
Is that possible to fetch only a number of bytes from some URL and then close the connection with urllib/urllib2? Or even may be a part from n-th byte to k-th? There is a page on that side and I don't need to load the whole page, only a piece of it. You can set the Range header to request a certain range of bytes, but you are dependent on the server to honor the request: import urllib2 req = urllib2.Request('http://www.python.org/') # # Here we request that bytes 18000--19000 be downloaded. # The range is inclusive, and starts at 0. # req.headers['Range']='bytes=%s-%s' % (18000, 19000) f =

bitfinex api v2 error, invalid key

微笑、不失礼 提交于 2019-12-05 17:06:50
I am trying to make a basic authenticated api call to their new v2 api and getting an invalid api key error returned. I reissued the api key just to verify, same error. from time import time import urllib.request import urllib.parse import hashlib import hmac APIkey = b'myapikeyyouarenotsupposedtosee' secret = b'myceeeeecretkeyyyy' url = 'https://api.bitfinex.com/v2/auth/r/wallets' payload = { #'request':'/auth/r/wallets', 'nonce': int(time() * 1000), } paybytes = urllib.parse.urlencode(payload).encode('utf8') print(paybytes) sign = hmac.new(secret, paybytes, hashlib.sha512).hexdigest() print

Urllib库

[亡魂溺海] 提交于 2019-12-05 17:04:39
python 之 Urllib库的基本使用 官方文档 https://docs.python.org/3/library/urllib.html 什么是Urllib Urllib是python内置的HTTP请求库 包括以下模块 urllib.request 请求模块 urllib.error 异常处理模块 urllib.parse url解析模块 urllib.robotparser robots.txt解析模块 urlopen 关于urllib.request.urlopen参数的介绍: urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None) url参数的使用 先写一个简单的例子: import urllib.request response = urllib.request.urlopen('http://www.baidu.com') print(response.read().decode('utf-8')) urlopen一般常用的有三个参数,它的参数如下: urllib.requeset.urlopen(url,data,timeout) response.read()可以获取到网页的内容,如果没有read(

Converting Unicode objects with non-ASCII symbols in them into strings objects (in Python)

[亡魂溺海] 提交于 2019-12-05 14:24:24
I want to send Chinese characters to be translated by an online service, and have the resulting English string returned. I'm using simple JSON and urllib for this. And yes, I am declaring. # -*- coding: utf-8 -*- on top of my code. Now everything works fine if I feed urllib a string type object, even if that object contains what would be Unicode information. My function is called translate . For example: stringtest1 = '無與倫比的美麗' print translate(stringtest1) results in the proper translation and doing type(stringtest1) confirms this to be a string object. But if do stringtest1 = u'無與倫比的美麗' and

Open Windows shared folder through linux machine

谁说我不能喝 提交于 2019-12-05 11:19:39
I am using python 2.5 on Ubuntu, and there's a machine in the same network called machine1 . The folder is shared. How to to get a file in a specific folder of that machine? I have tried, with no success: urllib.urlopen('\\machine1\folder\file.txt') Linux has a utiliy called smbmount , which can be found in package smbutils I believe. This is a command line utility which mounts a Windows share to a directory on the local machine, optionally with username/password. smbmount is I believe a utility which runs as root, so whether it's suitable for you I don't know. Maybe it can be used as user.

Change python byte type to string

五迷三道 提交于 2019-12-05 10:56:45
I'm using python to play with the stackoverflow API. I run the following commands: f = urllib.request.urlopen('http://api.stackoverflow.com/1.0/stats') d = f.read() The type of d is class 'bytes' and if I print it it looks like: b'\x1f\x8b\x08\x00\x00\x00 .... etc I tried d=f.read().decode('utf-8') as that is the charset indicated in the header, but I get a 'utf8' codec can't decode byte 0x8b in position 1" error message How do I convert the byte object I received from my urllib.request call to a string? David Check to make sure your response body is not gzipped. Believe its transfer encoding

Python 2.7.10 error “from urllib.request import urlopen” no module named request

て烟熏妆下的殇ゞ 提交于 2019-12-05 09:47:17
问题 I opened python code from github . I assumed it was python2.x and got the above error when I tried to run it. From the reading I've seen Python 3 has depreciated urllib itself and replaced it with a number of libraries including urllib.request . It looks like the code was written in python 3 (a confirmation from someone who knows would be appreciated.) At this point I don't want to move to Python 3 - I haven't researched what it would do to my existing code. Thinking there should be a urllib

how to convert characters like these,“a³ a¡ a´a§” in unicode, using python?

为君一笑 提交于 2019-12-05 09:29:31
i'm making a crawler to get text html inside, i'm using beautifulsoup. when I open the url using urllib2, this library converts automatically the html that was using portuguese accents like " ã ó é õ " in another characters like these "a³ a¡ a´a§" what I want is just get the words without accents contrã¡rio -> contrario I tried to use this algoritm, bu this one just works when the text uses words like these "olá coração contrário" def strip_accents(s): return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn')) Firstly, you have to ensure that your crawler

Asynchronously get and store images in python

久未见 提交于 2019-12-05 06:53:39
问题 The following code is a sample of non-asynchronous code, is there any way to get the images asynchronously? import urllib for x in range(0,10): urllib.urlretrieve("http://test.com/file %s.png" % (x), "temp/file %s.png" % (x)) I have also seen the Grequests library but I couldn't figure much if that is possible or how to do it from the documentation. 回答1: You don't need any third party library. Just create a thread for every request, start the threads, and then wait for all of them to finish