2012-08-15 2 views
0

모든 특정 웹 페이지를 가져 와서 배열로 전달하는 데 FINDALL을 사용해야합니다. 따옴표없이 링크 만 사용합니다. 배열이 아닌 경우 전달할 수있는 변수가 있습니다. 내가 한 번배열에 대한 파이썬 findall 변환

#!/usr/bin/env python 
import re,urllib,urllib2 

Url = "http://www.ihiphopmusic.com/music" 
print Url 
print 'test .............' 
req = urllib2.Request(Url) 
print "1" 
response = urllib2.urlopen(req) 
print "2" 
#reads the webpage 
the_webpage = response.read() 
#grabs the title 
the_list = re.findall(r'number-link" href="(.*?)#comments">0</a>',the_webpage) 
print "3" 
the_list = the_list.split(',') 
arrlist = array('c',the_list) 
print arrlist 

결과

http://www.ihiphopmusic.com/music 
test ............. 
1 
2 
3 
Traceback (most recent call last): 
    File "grub.py", line 17, in <module> 
    the_list = the_list.split(',') 
AttributeError: 'list' object has no attribute 'split' 
+0

당신은 Zalgo를 이렇게 깨울 것입니다 ... http://stackoverflow.com/a/1732454/53936 – JosefAssad

+1

html을 정규식으로 구문 분석하지 마십시오. 매우 쉽게 원하는 것을 성취 할 수있는 lxml 또는 BeautifulSoup 라이브러리를 사용하십시오. – Lanaru

답변

0

re.findall에 하나씩 또는 모든 하나를 사용할 수있는 루프에서 각각의 링크는 겹치지 않는 경기의 목록을 반환합니다. 리스트를으로 나눠서 AttributeError (list 개체에 split 메서드가 없음)가 표시되는 이유입니다. 나는 당신이 그걸로 성취하려는 것을 정확히 모르겠습니다. 개개의 성냥을 나눠서 반복 할 수 있도록 저장 하시겠습니까? 그렇다면, 당신은 같은 일을 할 수있는 :

나는 (내가 틀렸다면 정정 해줘), 당신이 이미있어 :) @mgilson가 지적 하듯이, 이미 목록입니다 무엇을 수집 할 수 있습니다에서
import itertools 
results = itertools.chain(*[x.split(',') for x in the_list]) 
0

:

#grabs the title 
the_list = re.findall(r'number-link" href="(.*?)#comments">0</a>',the_webpage) 
print "3" 
print type(the_list) 
print the_list 

그래서 당신은 당신이 원하는 일을하는 것을 반복 할 수 있습니다

for item in the_list: 
    print item 
+0

AweSome Works 대단히 고마워요. 나머지는 일해야합니다. – jokajinx

+0

행운을 빈다. 멋진 프로그램의 시작처럼 보입니다. :) – RocketDonkey

0

는 '분할'문자열 객체의 속성이 아닌 목록 객체입니다. AttributeError는 목록에서 split을 사용하려고 할 때 발생합니다. the_list를 인쇄하면 이미 목록임을 알 수 있습니다. 목록을 분할하고 각 URL을 별도의 줄에 표시하려면 print '\n'.join(the_list)을 사용할 수 있습니다.