changing value in python - python

I'm scraping some info from a site using python and one of the value's has to be in the name of the file.
for this specific part I can't seem to get it to print right.
In the API there is a line like this:
BroadcastDate = 20100401
now I want to print this value like this.
01.04.2010
I know there is a lot possible in Python with text but I can't seem to figure it out or find anything on Google.
you smart guys probably know if its possible and how.
if something is unclear or you have a question, let me know!
EDIT
so I got the following piece of code which should need to work in my head but I dont get a response:
b = "20100104"
print((b[7:8]).(b[5:6]).(b[1:4]))

Thanks #Barmar your awnser put me in the right direction.
This worked:
b = "20100103"
year = (b[0:4])
month = (b[4:6])
day = (b[6:9])
print ((day) +(".") +(month) +(".") +(year))

Related

In PRAW, is there a way to get the comment streams for multiple users?

With PRAW, I know you can do
subs = reddit.subreddit("Pics+Funny")
to get the streams of both r/Pics and r/Funny, and I know you can do this
user = reddit.redditor("spez")
to get one user, but is there a way to get multiple users at once? Like so:
users = reddit.redditor("spez+kn0wthing")?
I'm trying to read the comments of a large group of users, and I would like to know if something like this is possible. When I tried what I put right above this, it returned a 404 error. Could anyone help me out?
You cannot do this as you can with multireddits. However, there is a way around this with the pause_after parameter in praw comment streams.
e.g.
spezStream = reddit.redditor('spez').stream.comments(pause_after=-1)
kn0thingStream = reddit.redditor('kn0thing').stream.comments(pause_after=-1)
while True:
for comment in spezStream:
print(comment.body)
for comment in kn0thingStream:
print(comment.body)
And that should work :-)
Documentation here.
I don't believe there's a built-in way to do this. Either you'll need to loop through the users one at a time or create separate threads and merge the data.
for comment in reddit.redditor('spez+kn0thing').stream.comments(skip_existing=True):
#do stuff with comment
Should work. You can also add them as friends and stream comments from /r/friends

Python Xpath Contains %s in Selenium

I know this must be really, really simple but I can't quite figure out the syntax for this. I thought I understood but apparently I don't.
The XPath here works and if I use 'Wednesday' instead of '%s' it works fine but I'm trying to feed the XPath a variable string with this:
texttolookfor = "Wednesday"
buttonelement= webDriver.find_element_by_xpath("//tr[contains(., '%s')]/td[#class='button-cell']" % texttolookfor)
Which gives me:
<class 'selenium.common.exceptions.NoSuchElementException'>
But this works:
buttonelement= webDriver.find_element_by_xpath("//tr[contains(., 'Wednesday')]/td[#class='button-cell']")
So I've clearly misunderstood or made a mistake feeding the string in. I've been trying variations and looking at sample code but as far as my understanding goes this should of worked. I'd appreciate it if anyone could shed some light.
As asked in the comments I ran:
print "//tr[contains(., '%s')]/td[#class='button-cell']" % texttolookfor
which produced:
//tr[contains(., ' Wednesday')]/td[#class='button-cell']

Converting a List of Lat/Long coordinates to a business name

I havent used Python before but, am told this would be a good language to use for this process. I have a list of Lat/ Long coordinates that i need to convert into a business name. Any ideas where i might find documentation on how to complete this process?
Example:
Doc: LatLong.txt (Has a list of lat / long seperated by columns)
I need to run that list against the Places API with a max radius of 30 and return any businesses (BusinessName, Addy, Phone, etc.) within that radius.
from googleplaces import googleplaces
YOUR_API_KEY = 'aa262fad30e663fca7c7a2be9354fe9984f0b5f2'
google_places = googleplaces(YOUR_API_KEY)
query_result = google_places.nearby_search(lat_lng=41.802893, -89.649930,radius=20000)
for place in query_result.places:
print (place.name)
print (place.geo_location)
print (place.reference)
place.get_details()
print (place.local_phone_number)
print (place.international_phone_number)
print (place.webite)
print (place.url)
is what im playing around with..
Google has some pretty good documentation for getting started:
https://developers.google.com/places/training/basic-place-search
Also this answer may help: https://stackoverflow.com/a/21924452/1393496
You should edit your question and include more detail, what have you tried? what worked? what didn't work?

Python xgoogle library - site specific search

I am using the xgoogle python library to try to search as specific site. The code works for me when I do not use the "site:" indicator in the keyword search. If I do used it, the result set is empty. Does anyone have any thoughts how to get the code below to work?
from xgoogle.search import GoogleSearch, SearchError
gs = GoogleSearch("site:reddit.com fun")
gs.results_per_page = 50
results = gs.get_results()
print results
for res in results:
print res.title.encode("utf8")
print
A simple url with the "q" parameter (e.g. "http://www.google.com/search?&q=site:reddit.com+fun") works, so I assume it's some other problem.
If you are using pkrumins/xgoogle, a quick (and dirty) fix is to modify search.py line 240 as follows:
if not title or not url:
This is because Google changes their SERP layout, which breaks the _extract_description() function.
You can also take a look at this fork.
Put keyword before site:XX. It works for me.

Getting started with json

I have never worked with json before. I am trying: http://api.worldbank.org//topics?format=JSON and make things with it, but I don't even know how to get started.
Following some manuals, I did this:
import urllib
import urllib2
import simplejson
urlb = 'http://api.worldbank.org/topics'
datab = urllib2.urlopen(urlb+'?'+ param)
resultb = simplejson.load(datab)
but I have no clue of how to parse and work on it now, how do I list the individual items? count them? filter them?. Is there any simple tutorial that you guys can point me to or advice? I checked diveintopython, json's website and most of the obvious ones, but I am still struggling with it. Is there any simple step-by-step guide that somebody could point me to?
Thanks
Trying printing resultb. Its just a python list with dictionaries inside it. Treat it like you would any list.

Categories

Resources