urllib2

Reddit API returning useless JSON

早过忘川 提交于 2021-01-27 05:38:32
问题 I'm trying to scrape new stories from Reddit using their API and Python's urllib2, but I keep getting JSON documents like this one: { u'kind': u'Listing', u'data': { u'modhash': u'', u'children': [], u'after': None, u'before': None }} Here is my code: import json import time import urllib2 def get_submissions(after=None): url = 'http://reddit.com/r/all/new.json?limit=100' if after: url += '&after=%s' % after _user_agent = 'Reddit Link Analysis Bot by PirateLogic @ github.com/jamesbrewer'

Reddit API returning useless JSON

拈花ヽ惹草 提交于 2021-01-27 05:38:12
问题 I'm trying to scrape new stories from Reddit using their API and Python's urllib2, but I keep getting JSON documents like this one: { u'kind': u'Listing', u'data': { u'modhash': u'', u'children': [], u'after': None, u'before': None }} Here is my code: import json import time import urllib2 def get_submissions(after=None): url = 'http://reddit.com/r/all/new.json?limit=100' if after: url += '&after=%s' % after _user_agent = 'Reddit Link Analysis Bot by PirateLogic @ github.com/jamesbrewer'

Downloading a song through python-requests

瘦欲@ 提交于 2021-01-02 06:14:49
问题 I was trying to make a script to download songs from internet. I was first trying to download the song by using "requests" library. But I was unable to play the song. Then, I did the same using "urllib2" library and I was able to play the song this time. Can't we use "requests" library to download songs? If yes, how? Code by using requests: import requests doc = requests.get("http://gaana99.com/fileDownload/Songs/0/28768.mp3") f = open("movie.mp3","wb") f.write(doc.text) f.close() Code by

Fetching a .onion domain with requests

倾然丶 夕夏残阳落幕 提交于 2020-07-30 10:36:26
问题 I'm trying to access the following domain nzxj65x32vh2fkhk.onion using requests. I have tor running and I configured the session's object proxies correctly. import requests session = requests.session() session.proxies = {'http': 'socks5://localhost:9050', 'https': 'socks5://localhost:9050'} print(session.get('http://httpbin.org/ip').text) # prints {"origin": "67.205.146.164" } print(requests.get('http://httpbin.org/ip').text) # prints {"origin": "5.102.254.76" } However when I try to access

Fetching a .onion domain with requests

百般思念 提交于 2020-07-30 10:36:10
问题 I'm trying to access the following domain nzxj65x32vh2fkhk.onion using requests. I have tor running and I configured the session's object proxies correctly. import requests session = requests.session() session.proxies = {'http': 'socks5://localhost:9050', 'https': 'socks5://localhost:9050'} print(session.get('http://httpbin.org/ip').text) # prints {"origin": "67.205.146.164" } print(requests.get('http://httpbin.org/ip').text) # prints {"origin": "5.102.254.76" } However when I try to access

How to make while(True): two loops run at same time in python

十年热恋 提交于 2020-07-06 02:22:22
问题 Basically I need to run two while loops at the same time. The reason being that I need one loop to update the GUI, and the other to check if the program is connected to the internet. So maybe this requires multi-threading, or maybe I am just one webpage away to fixing my problem. Here's my source code: # -*- coding: utf-8 -*- import sys import urllib2 from Tkinter import * import time import socket def TheMainProgram(): airid = 'http://www.aviationweather.gov/metar/data?ids={airid}&format=raw

How to get the hidden input's value by using python?

随声附和 提交于 2020-07-05 08:37:05
问题 How can i get input value from html page like <input type="hidden" name="captId" value="AqXpRsh3s9QHfxUb6r4b7uOWqMT" ng-model="captId"> I have input name [ name="captId" ] and need his value import re , urllib , urllib2 a = urllib2.urlopen('http://www.example.com/','').read() thanx update 1 I installed BeautifulSoup and used it but there some errors code import re , urllib , urllib2 a = urllib2.urlopen('http://www.example.com/','').read() soup = BeautifulSoup(a) value = soup.find('input', {

Multipart form encoding and posting with urllib3

大憨熊 提交于 2020-06-28 07:16:59
问题 I'm attempting to upload a csv file to this site. However, I've encountered a few issues, and I think it stems from the incorrect mimetype (maybe). I'm attempting to manually post the file via urllib2 , so my code looks as follows: import urllib import urllib2 import mimetools, mimetypes import os, stat from cStringIO import StringIO #============================ # Note: I found this recipe online. I can't remember where exactly though.. #============================= class Callable: def _