Python 机械化,通过 url 跟随链接,nr 参数是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3569622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python mechanize, following link by url and what is the nr parameter?
提问by Rick
I'm sorry to have to ask something like this but python's mechanize documentation seems to really be lacking and I can't figure this out.. they only give one example that I can find for following a link:
我很抱歉不得不问这样的问题,但是 python 的机械化文档似乎真的很缺乏,我无法弄清楚。他们只给出了一个我可以通过以下链接找到的示例:
response1 = br.follow_link(text_regex=r"cheese\s*shop", nr=1)
But I don't want to use a regex, I just want to follow a link based on its url, how would I do this.. also what is "nr" that is used sometimes for following links?
但我不想使用正则表达式,我只想关注基于其 url 的链接,我该怎么做……还有有时用于关注链接的“nr”是什么?
Thanks for any info
感谢您提供任何信息
采纳答案by unutbu
br.follow_linktakes either a Linkobject or a keyword arg (such as nr=0).
br.follow_link接受一个Link对象或一个关键字 arg(例如nr=0)。
br.links()lists all the links.
br.links()列出所有链接。
br.links(url_regex='...')lists all the links whose urls matches the regex.
br.links(url_regex='...')列出所有 url 与正则表达式匹配的链接。
br.links(text_regex='...')lists all the links whose link text matches the regex.
br.links(text_regex='...')列出链接文本与正则表达式匹配的所有链接。
br.follow_link(nr=num)follows the numth link on the page, with counting starting at 0. It returns a response object (the same kind what br.open(...) returns)
br.follow_link(nr=num)跟随num页面上的第 th 个链接,从 0 开始计数。它返回一个响应对象(与 br.open(...) 返回的类型相同)
br.find_link(url='...')returns the Linkobject whose urlexactly equals the given url.
br.find_link(url='...')返回完全等于给定 url的Link对象url。
br.find_link, br.links, br.follow_link, br.click_linkall accept the same keywords. Run help(br.find_link)to see documentation on those keywords.
br.find_link, br.links, br.follow_link,br.click_link都接受相同的关键字。运行help(br.find_link)以查看有关这些关键字的文档。
Edit:If you have a target url that you wish to follow, you could do something like this:
编辑:如果您有一个想要关注的目标网址,您可以执行以下操作:
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
回答by jkerian
From looking at the code, I suspect you want
从查看代码,我怀疑你想要
response1 = br.follow_link(link=LinkObjectToFollow)
nr is the same as documented under the find_link call.
nr 与 find_link 调用中记录的相同。
EDIT: In my first cursory glance, I didn't realize "link" wasn't a simple link.
编辑:在我粗略的第一眼中,我没有意识到“链接”不是一个简单的链接。
回答by Rick
I found this way to do it, for reference for anyone who doesn't want to use regex:
我找到了这种方法,供不想使用正则表达式的任何人参考:
r = br.open("http://www.somewebsite.com")
br.find_link(url='http://www.somewebsite.com/link1.html')
req = br.click_link(url='http://www.somewebsite.com/link1.html')
br.open(req)
print br.response().read()
Or, it will work by the link's text also:
或者,它也可以通过链接的文本工作:
r = br.open("http://www.somewebsite.com")
br.find_link(text='Click this link')
req = br.click_link(text='Click this link')
br.open(req)
print br.response().read()
回答by Yuda Prawira
nris used for where exactly link you follow.
if the text or url you has been regex more than one.
default is 0so if you use default you will follow link first regex at all .
for example
the source :
nr用于您遵循的确切链接。如果您的文本或网址是正则表达式不止一个。默认值为0,因此如果您使用默认值,您将完全遵循链接第一个正则表达式。例如来源:
<a href="link.html>Click this link</a>
<a href="link2.html>Click this link</a>
in this example we need to follow "Click this link" text but we choose link2.html to follow exactly
在此示例中,我们需要遵循“单击此链接”文本,但我们选择 link2.html 以完全遵循
br.click_link(text='Click this link', nr=1)
by it you will get link2.html response
通过它你会得到link2.html响应

