2017-04-11 4 views
1

나는 chats 메시지를 긁어 모으기 위해 beautifulsoup를 사용하고 있지만 출력하라는 메시지가 표시되면 아무 것도 출력하지 않고 코드 0으로 종료합니다. 내가 뭘 잘못하고 있습니까? 크롬 네트워크 도구 또는 방화범을 oppening 때 당신이 자세히 본다면beautifulsoup4 찾을 수 없음 HTML

# import libraries, pip install beautifulsoup4. 
import urllib2 
from bs4 import BeautifulSoup 
import csv 
from datetime import datetime 

quote_page = 
'https://robertsspaceindustries.com/spectrum/community/SC/lobby/8' 

#finding 
page = urllib2.urlopen(quote_page) 
soup = BeautifulSoup(page, 'html.parser') 
name = soup.find('messages-items', attrs={'message-item status-default': 
'content'}) 
print name 

#logging 
with open('index.csv', 'a') as csv_file: 
    writer = csv.writer(csv_file) 
    writer.writerow([name, datetime.now()]) 

답변

1

, 당신은 당신의 웹 사이트가 당신이 원하는 데이터를 가져올 수있는 웹 서비스를 요청하는 것을 알 수 있습니다.

당신은 세 가지 매개 변수와 함께 게시물을 시뮬레이션해야합니다 : 마지막 ID가 새 메시지를받을 수신

  • before;
  • lobby_id 가져 오려는 현재 로비입니다.
  • 가져 오기 위해 얼마나 많은 메시지입니다
  • size

그것은 당신은 당신이 원하는 결과를 얻기 위해 분석해야하는에 JSON 개체를 반환합니다;

import requests 
import json 

response = requests.post('https://robertsspaceindustries.com/api/spectrum/message/history', data = {'before': None, 'lobby_id':'8', 'size':'50'}) 
lobby_data = json.loads(response.content.decode("utf-8")) 

for comment in lobby_data["data"]["messages"]: 
    print ("%s: %s" % (comment["member"]["displayname"], comment["content_state"]["blocks"][0]["text"])) 

출력 :

Antinov: Esp since spectrum doesn't even open a new tab to view large images.... 
Sir Quentin Reginald Watson: write a suggestion about it 
Antinov: As if CIG listens to those. 
Sir Quentin Reginald Watson: you will never know if you don't try 
.... 
여기

는 예제