Python-使用mechanize跟踪特定链接
我需要跟踪网站搜索的第一个结果 我打开网站的方式是从.csv文件中输入我要查找的名称,这样它就可以打开已经执行搜索的网站Python-使用mechanize跟踪特定链接,python,mechanize,Python,Mechanize,我需要跟踪网站搜索的第一个结果 我打开网站的方式是从.csv文件中输入我要查找的名称,这样它就可以打开已经执行搜索的网站 def name_to_url(name): words = name.split(" ") url = "http://website/search/results?Name=" end_of_url = "&Type=0&IncludeNlsp=True" for word in words: url +=
def name_to_url(name):
words = name.split(" ")
url = "http://website/search/results?Name="
end_of_url = "&Type=0&IncludeNlsp=True"
for word in words:
url += "%s+" % word
url += "%s" % end_of_url
return url
with open('file.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
open_page(name_to_url(row[0]))
我知道这可能不是最漂亮或最好的方法,但现在已经足够好了。我主要关心的是如何跟踪搜索返回的链接
假设名称为“Google”,搜索返回一个带有粗体绿色文本的链接,文本为“Google”。我已经看过mechanize,但是我不知道怎么做,主要是因为网站上的示例使用正则表达式有多种方法可以提取链接并使用
mechanize
。对于您来说,最简单的方法可能是使用以下方法通过索引获取:
browser.follow_link(nr=number)
或者,您可以使用browser.links()
按url\u regex
或text\u regex
筛选链接
另见: