Код: Выделить всё
import requestsКод: Выделить всё
from bs4 import BeautifulSoupКод: Выделить всё
import reКод: Выделить всё
brand = input(print("brand: "))Код: Выделить всё
if brand == 0:Код: Выделить всё
brand = NoneКод: Выделить всё
limit = int(input(print('limit: ')))Код: Выделить всё
URL = f"URL" #URL of the web page with {brand - number in the url} and {limit - max found results}Код: Выделить всё
HEADERS = {Код: Выделить всё
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"Код: Выделить всё
}Код: Выделить всё
def parse_forms():#parses specific forms which contain needed informationКод: Выделить всё
try:Код: Выделить всё
response = requests.get(URL, headers=HEADERS, timeout=15)Код: Выделить всё
response.raise_for_status()Код: Выделить всё
except requests.RequestException as e:Код: Выделить всё
print("Request failed:", e)Код: Выделить всё
return []Код: Выделить всё
soup = BeautifulSoup(response.text, "html.parser")Код: Выделить всё
forms = soup.find_all("form")#in my webpage they use to organize the elements of the page so i find information by this tagКод: Выделить всё
car_list = []Код: Выделить всё
for form in forms:Код: Выделить всё
car_data = {}Код: Выделить всё
# --- Extract hidden input values (often contain lot ID etc.)Код: Выделить всё
hidden_inputs = form.find_all("input", type="hidden")Код: Выделить всё
for inp in hidden_inputs:Код: Выделить всё
name = inp.get("name")Код: Выделить всё
value = inp.get("value")Код: Выделить всё
if name and value:Код: Выделить всё
car_data[name] = valueКод: Выделить всё
else:Код: Выделить всё
car_data[name] = "None"Код: Выделить всё
Код: Выделить всё
car_list.append(car_data)Код: Выделить всё
return car_listКод: Выделить всё
if __name__ == "__main__":Код: Выделить всё
cars = parse_forms()Код: Выделить всё
cars = cars[1:]#this operation removes unwanted infomationКод: Выделить всё
file_name = ""Код: Выделить всё
if int(brand) == 0:Код: Выделить всё
file_name = "car_list"Код: Выделить всё
else:Код: Выделить всё
file_name = car_sep[0]#first word of the array is always the brandКод: Выделить всё
output = open(f'{file_name}.txt', 'w', encoding="utf-8")Код: Выделить всё
output.write("Brand Model Year Body StartPrice Mileage IMG1 IMG2")Код: Выделить всё
output.write("\n")Код: Выделить всё
output.close()Код: Выделить всё
Код: Выделить всё
print(f"\nFound {len(cars)} form entries\n")Код: Выделить всё
car_sep = []Код: Выделить всё
for car in cars[:len(cars)-1]:Код: Выделить всё
car_sep.append(car['tomylistMARKA_NAME'])Код: Выделить всё
car_sep.append(car['tomylistMODEL_NAME'])Код: Выделить всё
car_sep.append(car['tomylistYEAR'])Код: Выделить всё
car_sep.append(car['tomylistKUZOV'])Код: Выделить всё
car_sep.append(car['tomylistSTART'])Код: Выделить всё
car_sep.append(car['tomylistMILEAGE'])Код: Выделить всё
car_sep.append(car['tomylistIMAGES'])Код: Выделить всё
car_sep.append(car['tomylistIMAGES2[]'])Код: Выделить всё
car_sep = ['_'.join(item.split(" ")) for item in car_sep]#i replace all the spaces with "_" to prepare for the future data editingКод: Выделить всё
output = open(f'{file_name}.txt', 'a', encoding="utf-8")Код: Выделить всё
for i in range (0, len(car_sep)):Код: Выделить всё
output.write(str(car_sep[i]))Код: Выделить всё
output.write(" ")Код: Выделить всё
Код: Выделить всё
output.write('\n')Код: Выделить всё
output.close()Код: Выделить всё
car_sep.clear()Код: Выделить всё
print(f"\nFound {len(cars)} form entries\n")#just to check how much information i have got from the page (idk why but this number deosn't exceed 501)Подробнее здесь: https://stackoverflow.com/questions/798 ... of-my-code
Мобильная версия