如何从 URL 列表中自动生成 URL+title?(使用 bash 或其他工具)

nev*_*nd9 2 url text-processing

使用 Linux Bash,如何转换文本文件:

http://example.org/
https://en.wikipedia.org/wiki/Main_Page
https://www.youtube.com/watch?v=mGQFZxIuURE
Run Code Online (Sandbox Code Playgroud)

进入:

http://example.org/ Example Domain
https://en.wikipedia.org/wiki/Main_Page Wikipedia, the free encyclopedia
https://www.youtube.com/watch?v=mGQFZxIuURE Mike Perry - The Ocean (ft. Shy Martin) - YouTube
Run Code Online (Sandbox Code Playgroud)

或进入:

http://example.org/
Example Domain

https://en.wikipedia.org/wiki/Main_Page 
Wikipedia, the free encyclopedia

https://www.youtube.com/watch?v=mGQFZxIuURE
Mike Perry - The Ocean (ft. Shy Martin) - YouTube
Run Code Online (Sandbox Code Playgroud)

?

怎么可以一个

  1. 从文件中的 URL 列表中提取 URL,
  2. 加载页面,
  3. 提取其页面标题,
  4. 在该 URL 的同一行或紧随其后的行上添加该 URL 之后的页面标题,然后

对该列表中的每个后续 URL 执行这些步骤 1-4?

如果不使用 Linux Bash,还有什么其他方法?

mur*_*uru 5

随着curl小狗

while IFS= read -r url
do
   printf "%s " "$url"
   curl -sL "$url" | # fetch the page
       pup 'head title:first-of-type text{}' # get the text of the first title tag in head
done < input
Run Code Online (Sandbox Code Playgroud)

  • 不是空行,只是末尾的换行符。这就是[一行的定义](https://unix.stackexchange.com/a/524442/70524) (2认同)