본문 바로가기
파이썬

파이썬 URL로 링크를 따르는 Python 기계화 및 nr 매개 변수는 무엇입니까?

by º기록 2020. 11. 8.
반응형

이런 질문을해서 미안하지만 파이썬의 기계화 문서가 정말 부족한 것 같고 이걸 알아낼 수 없습니다. 링크를 따라 가기 위해 찾을 수있는 한 가지 예만 제공합니다.

response1 = br.follow_link(text_regex=r"cheese\s*shop", nr=1)

하지만 정규식을 사용하고 싶지 않습니다. URL을 기반으로 한 링크를 따라 가고 싶습니다. 어떻게하면됩니까 .. 또한 링크를 따라가는 데 가끔 사용되는 "nr"은 무엇입니까?

정보 주셔서 감사합니다

 

해결 방법

 

br.follow_link Link 개체 또는 키워드 인수 (예 : nr = 0 )를받습니다.

br.links () 는 모든 링크를 나열합니다.

br.links (url_regex = '...') 는 URL이 정규식과 일치하는 모든 링크를 나열합니다.

br.links (text_regex = '...') 는 링크 텍스트가 정규식과 일치하는 모든 링크를 나열합니다.

br.follow_link (nr = num) 는 페이지의 num 번째 링크 다음에 0부터 시작합니다. 응답 객체를 반환합니다 (br.open과 동일한 종류). (...) 반환)

br.find_link (url = '...') url 이 주어진 URL과 정확히 일치하는 Link 객체를 반환합니다.

br.find_link , br.links , br.follow_link , br.click_link 는 모두 동일한 키워드를 허용합니다. 해당 키워드에 대한 문서를 보려면 help (br.find_link) 를 실행하십시오.

수정 : 팔로우하려는 타겟 URL이있는 경우 다음과 같이 할 수 있습니다.

import mechanize
br = mechanize.Browser()
response=br.open("http://www.example.com/")
target_url='http://www.rfc-editor.org/rfc/rfc2606.txt'
for link in br.links():
    print(link)
    # Link(base_url='http://www.example.com/', url='http://www.rfc-editor.org/rfc/rfc2606.txt', text='RFC 2606', tag='a', attrs=[('href', 'http://www.rfc-editor.org/rfc/rfc2606.txt')])
    print(link.url)
    # http://www.rfc-editor.org/rfc/rfc2606.txt
    if link.url == target_url:
        print('match found')
        # match found            
        break

br.follow_link(link)   # link still holds the last value it had in the loop
print(br.geturl())
# http://www.rfc-editor.org/rfc/rfc2606.txt

 

참조 페이지 https://stackoverflow.com/questions/3569622

 

 

반응형

댓글