Когда я использую браузер для сохранения этой страницы: http://maine.craigslist.org/fuo/ ссылки сохраняются таким образом, что они ссылаются на контент. например: href = "http://maine.craigslist.org/fuo/4323535885.html"
, когда я пытаюсь использовать wget, ссылки
$ wget --no-parent maine.craigslist.org/fuo
сохраняются как: href = "/ fuo / 4305913395.html"
Я пробовал варианты:
--spider
--page-requisites
--user-agent="Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:27.0) Gecko/20100101 Firefox/27.0"
, но все ссылки выходят без присоединенного URL.
У меня остальная часть сценария работает, чтобы разобрать мое местоположение и создать новый список ссылок на мебель в моем районе. Но я не могу понять, как получить тот же вывод, что и при сохранении страницы через Firefox.
Я думал, что использовать wget будет проще всего. Возможно, это не правильно. Если я смогу добиться того же эффекта с помощью другого программного обеспечения, если я смогу написать сценарий, чтобы он работал, я буду счастлив.
Опция --convert-links
должна делать то, что вы ищете:
wget --convert-links --no-parent maine.craigslist.org/fuo
Подробнее об этой опции и о том, что она делает, ниже (скопировано из man wget
) :
--convert-links After the download is complete, convert the links in the document to make them suitable for local viewing. This affects not only the visible hyperlinks, but any part of the document that links to external content, such as embedded images, links to style sheets, hyperlinks to non-HTML content, etc. Each link will be changed in one of the two ways: · The links to files that have been downloaded by Wget will be changed to refer to the file they point to as a relative link. Example: if the downloaded file /foo/doc.html links to /bar/img.gif, also downloaded, then the link in doc.html will be modified to point to ../bar/img.gif. This kind of transformation works reliably for arbitrary combinations of directories. · The links to files that have not been downloaded by Wget will be changed to include host name and absolute path of the location they point to. Example: if the downloaded file /foo/doc.html links to /bar/img.gif (or to ../bar/img.gif), then the link in doc.html will be modified to point to http://hostname/bar/img.gif. Because of this, local browsing works reliably: if a linked file was downloaded, the link will refer to its local name; if it was not downloaded, the link will refer to its full Internet address rather than presenting a broken link. The fact that the former links are converted to relative links ensures that you can move the downloaded hierarchy to another directory.
Если вы просматриваете источник веб-страницы в своем браузере, вы увидите, что ссылки в документе являются относительными. Браузер конвертирует их автоматически, но wget не может этого сделать.
Чтобы правильно отобразить страницу в браузере, можно использовать тег HTML < base> . Просто добавьте <base href="http://maine.craigslist.org/fuo">
в раздел head загруженных файлов, и все ссылки будут указывать на правильное абсолютное местоположение. Вы можете сделать это автоматически в вашем скрипте.
Если вы хотите, чтобы ваш скрипт выполнял дальнейшие действия над ссылками, лучшим решением было бы добавить скрипт к базовому URL перед каждой ссылкой.