【问题标题】:Edit and replace partial URLs in tumblr posts编辑和替换 tumblr 帖子中的部分 URL
【发布时间】:2015-09-10 18:08:41
【问题描述】:

我想更改我在 tumblr 中的 URL,但我的博客上到处都有硬编码链接。我被告知它可以自动化,而不是进入 1000 多个帖子并手动更新链接。我需要它:

  1. 访问博客页面
  2. 检查帖子超链接文本中的旧网址
  3. 如果存在,请单击编辑以编辑帖子内容
  4. 单击文本区域中的 URL
  5. 在出现的弹出窗口中点击编辑
  6. 仅将弹出窗口中的部分 URL 替换为新 URL(例如:如果我们以 http://old.tumblr.com/tagged 开头,则需要 http://new.tumblr.com/tagged
  7. 单击弹出窗口上的完成以将其关闭并保存 URL 更改
  8. 保存对帖子的更改
  9. 继续查看页面上的下一篇文章
  10. 如果没有更多实例发生,请继续到下一页
  11. 重复直到到达最后一页

所以我相信我了解所需的逻辑/步骤,但我的缺陷在于能够执行它们。实现这一点的最佳语言或方法是什么?直截了当的首选,因为我是一个完整的编码新手。有人向我提到了 Python。自动热键也可以吗?

如果这不是问的正确地方,我深表歉意。

目前,我在旧 URL 的页面上设置了重定向。

<title>Redirect</title>
<script>location.replace('http://new.tumblr.com' + location.pathname);</script>
<noscript>
<h1>This blog has moved to <a href="http://new.tumblr.com/">New Blog</a>.</h1>
<p>If you&rsquo;re reading this, you have JavaScript turned off and therefore can&rsquo;t be redirected automatically. Replace &ldquo;{BlogURL}&rdquo; with &ldquo;http://{text:New Tumblr URL}.tumblr.com/&rdquo; in your browser&rsquo;s address bar to get to your destination.</p>
</noscript>

【问题讨论】:

  • 你能展示你的尝试吗?
  • 现在我在旧 URL 的页面上设置了一个重定向
  • @mikedidthis - 我在上面的帖子中添加了我正在使用的重定向代码。我猜它完成了工作。

标签: python url tumblr autohotkey


【解决方案1】:

在 AutoHotkey 中我会使用 IE COM 自动化来完成工作,它会是最可靠的。

Com Object Reference

编辑:

坦率地说,使用浏览器自动化方法编辑 HTML 是一种非常低效的方法。如果您有权访问该站点,则可能可以直接访问上传 HTML 文件。如果是这种情况,下面的代码应该为您提供有关如何编辑页面中包含的链接的足够详细信息。

下面的代码简化了您将要做的事情。只是为了熟悉这个过程。

html =
(
<html>
<body>
<a href="http://old.tumblr.com/tagged1"/>this old link</a>
<a href="http://old.tumblr.com/tagged2"/>this old link two</a>
<a href="http://old.tumblr.com/tagged3"/>this old link three</a>
<a href="http://new.tumblr.com/tagged3"/>this new link</a>
</body>
</html>
)

pwb := ComObjCreate("HTMLfile"), pwb.Write( html )
Links := pwb.Links
Loop % Links.Length ; check each link
        If ((RelatedLink := Links[A_Index-1].href) != "" && (Links[A_Index-1].href ~= "http://old.")) { ; if the link is not blank
               Links[A_Index-1].href := StrReplace(Links[A_Index-1].href, "http://old.", "http://new.")
            }

html := pwb.documentElement.innerHTML
MsgBox % html

这就是我将它应用到一堆网站的方式:

SetBatchLines -1
fileName := A_ScriptDir . "\myfile.txt"

MyListOfWebPages = ; add all your blog page urls here
(
http://myblogpageone.html
http://myblogpagetwo.html
http://myblogpagethree.html
http://myblogpagefour.html
)

For Each, Line in StrSplit(MyListOfWebPages, "`n", "`r") {
          FileAppend, % GrabWebPage(Line), % A_scriptDir "\htmlfile" A_index ".html"
}

GrabWebPage(Webpage) {
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
;Change below to your URL
whr.Open("GET", Webpage, true) 
whr.Send()
whr.WaitForResponse()
pwb := ComObjCreate("HTMLfile"), pwb.Write( whr.ResponseText ) 

Links := pwb.Links ; collection of hyperlinks on the page
   Loop % Links.Length ; check each link
        If ((RelatedLink := Links[A_Index-1].href) != "" && (Links[A_Index-1].href ~= "http://old.")) { ; if the link is not blank
               Links[A_Index-1].href := StrReplace(Links[A_Index-1].href, "http://old.", "http://new.")
            }
    Return pwb.documentElement.innerHTML
}

【讨论】:

  • 我会检查的。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-15
相关资源
最近更新 更多