2011-08-24 4 views
2

여기까지 가지고있는 스크립트의 (엉망)입니다. http://www.j-archive.com/showseason.php?season=27웹 사이트에서 Jeopardy 질문 및 답변을 추출하는 방법은 무엇입니까?

내 일반적인 접근은 사이비 코드 I을 따르도록했다 : http://pastebin.com/prpdJXsq

#Jeopardy! 
#Goal is to create a list of lists ie. 
#[[Category 1, Question 1, Answer 1], [Category 1, Question 2, Answer 2]] 
#First iteration will just be Q 


import urllib.request, re 

Question = [] 

first_game_id = 3458 
last_game_id = 3713 

for gameid in range(first_game_id, last_game_id): 
    webpageid = "http://www.j-archive.com/showgame.php?game_id=" + str(gameid) 
    temp=urllib.request.urlopen(webpageid) 
    webpage=temp.read() 
    temp.close() 
    for line in webpage: 
     if question != None: 
      Question.append(question) 
print(Question) 

#wrong. ??? = figure out which re to insert? 

question = re.match('clue_text\"></td>') 
answer= re.match'correct_response&quot;&gt;???&' 


#trying to use re match and compile to match the string and output tuple? 
import urllib.request, re 
webpageid = "http://www.j-archive.com/showgame.php?game_id=" + str(3713) 
temp=urllib.request.urlopen(webpageid) 
webpage=temp.read() 
temp.close() 

question=re.compile(r'clue_text">*?</td>') 

Question = [] 
## 
##for line in webpage: 
## print(line) 
## 
## if question.match(line) != None: 
##  Question.append(question) 
## 
##print(Question) 

내가 초보자 해요 (고작)이 멋진 웹 사이트에서 모든 퍼디 질문/대답을 추출하는 파이썬 스크립트를 작성하려고 비슷한 질문에 대한 답변으로 여기에 있지만 이것은 내가 얻은 것입니다 : Jeopardy questions in Excel or other database format?

건설적인 비평이나 철저한 경멸감이 크게 감사 할 것입니다.

+0

당신이 문제가뿐만 아니라 코드를 게시하고 당신이 무슨 말을 하려는지에 연결하고 특정 일을 적어주십시오 해야 할 것. – agf

+0

+1 당신이 얻으려고하는 것의 순수한 무작위성을 위해서 – ghostJago

+1

사이드 노트로서, 'urllib'을 사용하여 각 웹 페이지를 순차적으로 다운로드하는 대신에, 이것은 전체 웹 사이트를 다운로드하기 위해'wget' 유틸리티를 사용하는 좋은 기회가 될 것입니다. 디스크. 그런 다음 인터넷 연결 속도가 느린 대신 하드 디스크 속도 (빠름)로 데이터를 처리 할 수 ​​있습니다 (느리게). –

답변

2

내가 lxml를 사용하는 것이 좋습니다, 그것은 XPath support의 활용 것 :

import lxml.html 

doc = lxml.html.parse('http://www.j-archive.com/showgame.php?game_id=1') 
# get all td's with class="clue_text", these are the clues 
clues = doc.xpath('//td[@class="clue_text"]') 
# create a dict of clue_id, clue_text 
clues_by_id = dict((x.attrib['id'], x.text) for x in clues) 
관련 문제