Загрузка файлов, которых нет на веб-странице, на которой они размещены

Я включил два (слегка различающихся )скрипта, которые должны делать то, что вы хотите :скрипт Python и скрипт Bash.

Решение Python

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""prefix_join.py"""

import sys

input_1 = sys.argv[1]
input_2 = sys.argv[2]

# Initialize a list to store prefixes as they occur
prefix_list = list()

# Parse the first input file
data_1 = dict()
with open(input_1, "r") as file_1:
    for line in file_1:

        # Remove trailing and leading whitespace
        line = line.strip()

        # Split the string on the first whitespace character
        prefix, sep, suffix = line.partition(" ")

        # Add the prefix the list of prefixes
        if prefix not in prefix_list:
            prefix_list.append(prefix)

        # Add the prefix to the first data dictionary
        if prefix not in data_1:
            data_1[prefix] = list()

        # Add the suffix to the data dictionary
        data_1[prefix].append(suffix)

# Parse the second input file
data_2 = dict()
with open(input_2, "r") as file_2:
    for line in file_2:

        # Remove trailing and leading whitespace
        line = line.strip()

        # Split the string on the first whitespace character
        prefix, sep, suffix = line.partition(" ")

        # Add the prefix the list of prefixes
        if prefix not in prefix_list:
            prefix_list.append(prefix)

        # Add the prefix to the first data dictionary
        if prefix not in data_2:
            data_2[prefix] = list()

        # Add the suffix to the data dictionary
        data_2[prefix].append(suffix)

# Output the joined data
for prefix in prefix_list:
    for value_1 in data_1.get(prefix, list()):
        for value_2 in data_2.get(prefix, list()):
            output_line = "{} {} X {}".format(prefix, value_1, value_2)
            print(output_line)

Вы бы запустили это так:

python prefix_join.py file-1.txt file-2.txt

Для данных вашего примера он выдает следующий результат:

22:50:48] Return_M X <0> X led_required
22:50:48] Return_M X <0> X start_rules
22:50:48] Return_M X <0> X leadstart
22:50:49] Return_A X <0> X asynchronous_start
22:50:49] Return_A X <0> X controldown
22:50:49] Return_A X <0> X select_set(3)
22:50:49] Return_A X <0> X limiting_rules
22:50:50] Return_F X <0> X stock_manager
22:50:50] Return_F X <0> X led_blink
22:50:50] Return_F X <0> X start_required
22:50:51] Return_K X <0> X control_down
22:50:51] Return_K X <0> X select_set(3)
22:50:51] Return_K X <0> X start_rules
22:50:52] Return_Y X <0> X stock_manager
22:50:52] Return_Y X <0> X blink_led

Решение Баша

#!/usr/bin/env bash
# prefix-join.sh

# Get the input files as command-line arguments
input_file_1="$1"
input_file_2="$2"

# Set the internal field separator to be a newline (don't include spaces or tabs)
IFS=$'\n'

# Iterate over the lines of the first input file
for line_1 in $(cat "${input_file_1}"); do

    # Split the line on the first space
    prefix_1="${line_1/ */}";
    suffix_1="${line_1#*] }";

    # Iterate over the lines of the second input file
    for line_2 in $(cat "${input_file_2}"); do

        # Split the line on the first space
        prefix_2="${line_2/ */}";
        suffix_2="${line_2#*] }";

        # If the prefixes agree, combine the suffixes and output the result
        if [[ "${prefix_1}" = "${prefix_2}" ]]; then
            echo "${prefix_1} ${suffix_1} X ${suffix_2}"
        fi 

    done
done

Вы бы запустили это так:

bash prefix-join.sh file-1.txt file-2.txt

Для данных примера это дает тот же результат, что и сценарий Python.

0
22.01.2021, 14:27
1 ответ

URL-адреса PDF встроены в атрибуты javascript onclick. Вы можете grepэти:

wget -qO- https://www.lezioni4all.com/ase/appunti | 
  grep -o "https://www\.lezioni4all\.com/files/[^']*\.pdf"

Вы можете загрузить их, используя опцию «нет» -clobber -nc(, так как каждый документ связан дважды )и-i:

wget -nc -P ~/Desktop/ASE_lezioni4all -i <(
  wget -qO- https://www.lezioni4all.com/ase/appunti |
  grep -o "https://www\.lezioni4all\.com/files/[^']*\.pdf")
0
18.03.2021, 22:35

Теги

Похожие вопросы