Парсинг сайта - Python
Формулировка задачи:
Добрый день. Возникла проблема с написанием рекурсивного парсинга страниц сайта.
Суть такова, написан код, который парсит только страницу, которую я указал. Помогите разобраться как реализовать, чтобы можно было извлечь абсолютно все ссылки с сайта
Листинг программы
- import requests
- from bs4 import BeautifulSoup, SoupStrainer
- from itertools import groupby
- class TRaCAse:
- url='http://corp.fastsite.ru/'
- file = open('links.txt', 'w')
- req = requests.get(url)
- soup = BeautifulSoup(req.content, 'html.parser', parse_only=SoupStrainer('a'))
- list_1= ([link['href'] for link in soup if link.has_attr('href')])
- list_1 = list(set(list_1))
- deleted = 0
- for i in range(len(list_1)):
- if "https" not in list_1[i - deleted] :
- if "http" not in list_1[i - deleted] :
- if "www" not in list_1[i - deleted] :
- del list_1[i - deleted]
- deleted += 1
- ##удаление повтор. ссылок
- new_t = [el for el, _ in groupby(list_1)]
- string_x = ''.join(str(e) + '\n' for e in new_t)
- file.write(string_x)
- file.close()
Решение задачи: «Парсинг сайта»
textual
Листинг программы
- url = 'http://corp.fastsite.ru/'
- resp = requests.get(url)
- tree = lxml.html.fromstring(resp.text)
- xpath = './/a[(starts-with(@href,"http") or starts-with(@href,"https")) and contains(@href,"www")]/@href'
- links = set(tree.xpath(xpath)) # set удалит дубликаты
- print(links)
- # или так
- links = tree.xpath('.//a[@href]/@href')
- links = set([href for href in links if re.match(r'(https?)://(?=www)',href)])
- print(links)
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д