Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-使用mechanize跟踪特定链接_Python_Mechanize - Fatal编程技术网

Python-使用mechanize跟踪特定链接

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 +=

我需要跟踪网站搜索的第一个结果

我打开网站的方式是从.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 += "%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
筛选链接

另见:

请参见此处: