The use of a watercress API--rejecting the mindset

Source: Internet
Author: User

Haven't written a blog for a long time, recently has been using the Watercress API crawl data, do not know what was the previous, after all, just start with not long, with the latest V2 version, the previous not updated, you can refer to https://developers.douban.com/wiki/?title= API_V2, but in the use of some APIs will be some problems, you need to ponder, because there is not much information on the API crawling data, today talk about some watercress with the city V2 an interface call-get the list of activities.

According to the interface provided by the Watercress V2, get the list of activities:GET https://api.douban.com/v2/event/list,返回值是eventlist,其格式可以自己查询,我们试一下:

Try :         = Urllib2.urlopen (R'https://api.douban.com/v2/event/list')                           except  Exception as ex:    print= json.loads (html.read ()) EventList (Hjson) 

This is the main code, EventList is defined as a function, output activity list, run, error, 400bad request, or do not know what reason, jump from the Web page https://api.douban.com/v2/event/list, Get a file, open with a browser, display information for

{"MSG": "Invalid_parameter", "code": 1016, "request": "GET \/v2\/event\/list"}

, the parameter is wrong, my God, what ghost, do not follow the API said, how wrong? Can't get the list to work? Try getting a list of cities:

Try :         = Urllib2.urlopen (R'https://api.douban.com/v2/loc/list')                except  Exception as ex:    print= json.loads (html.read ()) City_print (Hjson) 

City_print for the definition of the output city list of functions, run, perfect output, brother, blindfolded. What is the reason, Baidu, no progress, or Google a bit (hehe), but also rely on their own. Looking for ideas from the source, open the watercress, into the same city sub-module, found that the upper left corner of your location, the brain flashed a thought: will it be acquired when the name of the designated place? Try it now, but how to designate a place name, think of a few days before climbing watercress film top250 when there is a start parameter, this is not also useful, which words to use. In the see Get the city list of the interface, is loc/list, that designated place name also use this try, say dry do:

Try :         = Urllib2.urlopen (R'https://api.douban.com/v2/event/list?loc=qingdao')                                                       except Exception as ex:     Print  = json.loads (html.read ()) EventList (Hjson)

Because I am in Qingdao, I designate the place name as Qingdao, run for a while, look at some data briefly:

-----------------------
Owner_name: Cultural institution of companion book Shop
Owner_uid:liangyoubooks
owner_id:121533
id:26343442
begin_time:2016-03-06 10:00:00
end_time:2016-05-04 22:00:00
Address: B1, Hisense Plaza, No. 117th, Macau Road, South Hong Kong Road, Shinan District, Qingdao, China the space of a companion book
----------------------
Owner_name: I love the outdoors.
owner_uid:128609
owner_id:128609
id:26248720
Begin_time:2016-02-21 08:30:00
End_time:2016-05-15 16:00:00
Address: Qingdao Laoshan District Sand Mouth Plaza
----------------------
Owner_name: Photographer Jia
Owner_uid:lifephotojia
owner_id:103740178
Id:25637123
BEGIN_TIME:2016-04-17 08:00:00
End_time:2016-07-15 17:00:00
Address: Qingdao South District Cham Shan/Taiping Kok suitable for shooting any location
----------------------

Perfect! Encounter the problem of their own solution to the feeling of a word--cool! The complete code is also put out, write a bit clumsy, big God do not look.

#---coding:utf-8---"""date:2016-4-17language:python2.7.6by seven_clear"""ImportJSONImportUrllib2ImportstringdefEventList (JSON):'Output Activity List'    Print 'Count:', json['Count']    Print 'Total:', json[' Total']    Print '-----------------------'     forEventinchjson['Events']:        Print 'owner_name:', event['owner']['name']        Print 'Owner_uid:', event['owner']['UID']        Print 'owner_id:', event['owner']['ID']        Print 'ID:', event['ID']        #print ' content: ', event[' content '        Print 'Begin_time:', event['Begin_time']        Print 'End_time:', event['End_time']        Print 'Address:', event['Address']        Print '----------------------'defCity_print (JSON):'Output City List'    Print 'Count:', json['Count']    Print 'Total:', json[' Total']    Print '----------------------------'     forCityinchjson['locs']:        Print 'Parent:', city['Parent']        Print 'ID:', city['ID']        Print 'Name:', city['name']        Print 'UID:', city['UID']        Print '----------------------------'Try: HTML= Urllib2.urlopen (r'Https://api.douban.com/v2/event/list?loc=qingdao')                           #https://api.douban.com/v2/loc/list ')                           exceptException as ex:Printex#print html.read ()Hjson=json.loads (Html.read ()) EventList (Hjson)#city_print (Hjson) forKeyinchHjson:#Test JSON format    PrintKey

The use of a watercress API--rejecting the mindset

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.