1

First time access the Javascript, it looks like:

please find https://xueqiu.com/v4/statuses/user_timeline.json?page=1&user_id=4357540281

{ count: 20
, statuses: 
  [ { id            : 180
    , user_id       : 435
    , source        : '111'
    , title         : 'aa'
    , created_at    : 1621
    , retweet_count : 0
    , reply_count   : 1
    , Timebefore    : '05-20 5-15 8:00'
...
      id            : 181
    , user_id       : 436
    , source        : '111'
    , title         : 'bb'
    , created_at    : 1621
    , retweet_count : 0
    , reply_count   : 1
    , Timebefore    : '05-19 5-15 8:00'

I only want to choose value under the condition: Timebefore contains value:'5 -20'.And no idea why could I achieve attribute selection. In selenium, I can try like text contains sth, here I wanna it retuns timebefore contains s specific date. I tried:

with requests.Session() as connection:
    connection.headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.86 YaBrowser/21.3.0.740 Yowser/2.5 Safari/537.36"
    _ = connection.get("https://xueqiu.com")
    user_timeline = connection.get("https://xueqiu.com/v4/statuses/user_timeline.json?page=1&user_id=4357540281").json()
    for status in user_timeline["statuses"]:
    status["TimeBefore": '05-20']

but it retuns: TypeError: unhashable type: 'slice'. Thanks in advance!

Joyce
  • 435
  • 4
  • 13
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Life is complex May 21 '21 at 03:22
  • thanks, but unfortunately that cannot answer the question.. – Joyce May 21 '21 at 03:25

1 Answers1

1

The best was to solve this is to walk the keys and values. This allows you to look at the structure of your JSON.

import requests

with requests.Session() as connection:
    connection.headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.86 YaBrowser/21.3.0.740 Yowser/2.5 Safari/537.36"
    _ = connection.get("https://xueqiu.com")
    user_timeline = connection.get("https://xueqiu.com/v4/statuses/user_timeline.json?page=1&user_id=4357540281").json()
    for status in user_timeline['statuses']:
        date_string = str(status['timeBefore']).split(" ")[0]
        if '05-20' in date_string:
           print(date_string)
           # output
           05-20
           05-20
           05-20
           truncated...

Life is complex
  • 15,374
  • 5
  • 29
  • 58