【问题标题】:Python - Using mechanize to follow a specific linkPython - 使用 mechanize 跟踪特定链接
【发布时间】:2015-03-24 22:55:51
【问题描述】:

我需要关注网站上的第一个搜索结果。

我通过从 .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 但我不知道怎么做,主要是因为网站上的示例使用了正则表达式

【问题讨论】:

标签: python mechanize


【解决方案1】:

有多种方法可以提取链接并使用mechanize 跟随它。可能对您有用的最简单的选择是使用以下索引通过索引获取它:

browser.follow_link(nr=number)

或者,您可以使用browser.links() 过滤掉url_regextext_regex 的链接。

另见:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-24
    • 2011-05-14
    • 1970-01-01
    • 2023-04-08
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多