json
  • web-scraping
  • scrapy
  • 2017-01-31 3 views 0 likes 
    0

    div 값을 가진 json 요청을 처리하고 있습니다. 는 지금은 데이터 내용 값Scrapy Extract 태그의 값

    <li id="term_100800962" data-content-value='{"nl_term_id":100800962,"c_price_from":33415,"nd_price_discount":0,"nl_tour_id":1017864,"nl_hotel_id":[49316],"d_start":"2017-04-12","d_end":"2017-04-17"}' > 
    

    의 값을 얻을 '날짜', 'ID', '가격'에 저장하려면 내가 할 수있는 방법을 알아낼 수 없습니다.

    쉬운 방법이 있습니까?

    답변

    1
    In [2]: from scrapy.selector import Selector 
    
    In [3]: text = """<li id="term_100800962" data-content-value='{"nl_term_id":100 
        ...: 800962,"c_price_from":33415,"nd_price_discount":0,"nl_tour_id":1017864," 
        ...: nl_hotel_id":[49316],"d_start":"2017-04-12","d_end":"2017-04-17"}' >""" 
    
    In [4]: sel = Selector(text=text) 
    
    In [5]: data_string = sel.xpath('//li/@data-content-value').extract_first() 
    
    In [6]: import json 
    
    In [7]: json.loads(data_string) 
    Out[7]: 
    {'c_price_from': 33415, 
    'd_end': '2017-04-17', 
    'd_start': '2017-04-12', 
    'nd_price_discount': 0, 
    'nl_hotel_id': [49316], 
    'nl_term_id': 100800962, 
    'nl_tour_id': 1017864} 
    

    먼저 속성의 문자열을 가져온 다음 json.loads()을 사용하여 python dict로 변환하십시오.

    , 우리가 부하에게 JSON에 대한 모든 응답을해야 JSON 응답을 반환하고 우리가 필요로하는 정보를 선택합니다이 URL :

    In [11]: fetch('https://dovolena.invia.cz/direct/tour_search/ajax-next-boxes/?nl 
    ...: _country_id%5B0%5D=28&nl_locality_id%5B0%5D=19&d_start_from=23.01.2017& 
    ...: d_end_to=19.04.2017&nl_transportation_id%5B0%5D=3&sort=nl_sell&page=1&g 
    ...: etOptionsCount=true&base_url=https%3A%2F%2Fdovolena.invia.cz%2F') 
    
    In [12]: j = json.loads(response.text) 
    In [15]: j['boxes_html'] # this will renturn the html in json file. 
    In [15]: from scrapy.selector import Selector 
    
    In [16]: sel = Selector(text=j['boxes_html']) # loads html to selector 
    
    In [17]: datas = sel.xpath('//li/@data-content-value').extract() # return all data in a list 
    In [21]: [json.loads(d) for d in datas] # loads text to value 
          |---dict-----| 
    # this will return a list of dict which generated by json.loads(d), and you can use json.loads(d)['d_end'] to access it's element. 
    

    아웃 : 난 때

    [{'c_price_from': 15690, 
        'd_end': '2017-04-16', 
        'd_start': '2017-04-09', 
        'nd_price_discount': 27, 
        'nl_hotel_id': [24810], 
        'nl_term_id': 93902083, 
        'nl_tour_id': 839597}, 
    {'c_price_from': 27371, 
        'd_end': '2017-04-17', 
        'd_start': '2017-04-12', 
        'nd_price_discount': 4, 
        'nl_hotel_id': [49316], 
        'nl_term_id': 100804770, 
        'nl_tour_id': 1017864}, 
    {'c_price_from': 32175, 
        'd_end': '2017-04-17', 
        'd_start': '2017-04-12', 
        'nd_price_discount': 4, 
        'nl_hotel_id': [49316], 
        'nl_term_id': 100800962, 
        'nl_tour_id': 1017864}, 
    
    +0

    내가 오류를 얻고있다 이걸 시험해 봐. – Kostas

    +0

    당신이 저를 도와 줄 수 있다면 기꺼이 될 것입니다. [link] (http://pastebin.com/MYuER5xf) – Kostas

    +0

    내가 직면하고 있다고 생각하는 마지막 오류를 확인하십시오. 나는 내가 별도로 저장하고 싶다는 것을 위에 썼다. 그러나 doesnt는 나를 허락한다. 나는 이것이 나의 오해를 유감스럽게 생각하는 마지막 단계라고 생각한다. [link] (http://imgur.com/a/brMVq) – Kostas

    관련 문제