2011-04-01 2 views
2

텍스트에 "Display charges"라는 링크가> 1 인 페이지가 있습니다. 내가 마지막 링크를 찾을 수 있도록python mechanize find_link 최종 일치하는 링크 찾기

firstLink = br.find_link(text_regex=re.compile("Display charges"),nr=0) 

내가 사랑하는 것과 최초의 링크를 찾을 수 있습니다. 나는 이것이 일하기를 바랐다.

lastLink = br.find_link(text_regex=re.compile("Display charges"),nr=-1) 

그러나 일치하는 하나의 링크 만있는 경우 실패했다.

참고 : 파이썬과 초보자를 기계화하지만 도움을 발견 한 큰 돌파구 :

답변

7

그런 다음, 그러한 모든 링크를 생성 list(...)[-1]를 사용하는 br.links()을 사용할 수있는 마지막 하나를 선택하는 것이 었습니다 (mechanize.Browser) 예를 들면 다음과 같습니다

lastLink = list(br.links(text_regex=re.compile("Display charges")))[-1] 

:

In [29]: import mechanize 

In [30]: import re 

In [31]: br=mechanize.Browser() 

In [32]: br.open('http://www.example.com') 
Out[32]: <response_seek_wrapper at 0xa2b59ec whose wrapped object = <closeable_response at 0xa2b554c whose fp = <socket._fileobject object at 0xa3143ac>>> 

In [33]: br.links() 
Out[33]: <generator object __call__ at 0xa289af4> 

In [34]: list(br.links()) 
Out[34]: 
[Link(base_url='http://www.iana.org/domains/example/', url='/', text='Homepage[IMG]', tag='a', attrs=[('href', '/')]), 
Link(base_url='http://www.iana.org/domains/example/', url='/domains/', text='Domains', tag='a', attrs=[('href', '/domains/')]), 
Link(base_url='http://www.iana.org/domains/example/', url='/numbers/', text='Numbers', tag='a', attrs=[('href', '/numbers/')]), 
Link(base_url='http://www.iana.org/domains/example/', url='/protocols/', text='Protocols', tag='a', attrs=[('href', '/protocols/')]), 
Link(base_url='http://www.iana.org/domains/example/', url='/about/', text='About IANA', tag='a', attrs=[('href', '/about/')]), 
Link(base_url='http://www.iana.org/domains/example/', url='/go/rfc2606', text='RFC 2606', tag='a', attrs=[('href', '/go/rfc2606')]), 
Link(base_url='http://www.iana.org/domains/example/', url='/about/', text='About', tag='a', attrs=[('href', '/about/')]), 
Link(base_url='http://www.iana.org/domains/example/', url='/domains/', text='Domains', tag='a', attrs=[('href', '/domains/')]), 
Link(base_url='http://www.iana.org/domains/example/', url='/protocols/', text='Protocols', tag='a', attrs=[('href', '/protocols/')]), 
Link(base_url='http://www.iana.org/domains/example/', url='/numbers/', text='Number Resources', tag='a', attrs=[('href', '/numbers/')]), 
Link(base_url='http://www.iana.org/domains/example/', url='http://www.icann.org/', text='Internet Corporation for Assigned Names and Numbers', tag='a', attrs=[('href', 'http://www.icann.org/')]), 
Link(base_url='http://www.iana.org/domains/example/', url='mailto:[email protected]?subject=General%20website%20feedback', text='[email protected]', tag='a', attrs=[('href', 'mailto:[email protected]?subject=General%20website%20feedback')])] 

In [35]: list(br.links(text_regex=re.compile("About"))) 
Out[35]: 
[Link(base_url='http://www.iana.org/domains/example/', url='/about/', text='About IANA', tag='a', attrs=[('href', '/about/')]), 
Link(base_url='http://www.iana.org/domains/example/', url='/about/', text='About', tag='a', attrs=[('href', '/about/')])] 
+0

이 나를 기계화를 시작 도왔다. 감사! – jamescampbell

관련 문제