Как очистить и извлечь все имена подкатегорий со всех связанных страниц для категории википедии, используя python 3.6? - PullRequest
0 голосов
/ 06 октября 2018

Я хочу очистить все подкатегории и страницы под заголовком категории на странице категории: "Категория: Информатика".Ссылка на то же самое выглядит следующим образом: http://en.wikipedia.org/wiki/Category:Computer_science.

Я получил представление о вышеупомянутой проблеме из следующего ответа о переполнении стека, который указан в следующей ссылке. Pythonic beautifulSoup4: Как получить оставшиеся заголовки из ссылки на следующую страницу категории википедии и Как очистить подкатегории и страницы в категориях страницы категории википедии с помощью Python

Однако ответ не полностью решает проблему.Это только царапает Pages in category "Computer science".Но я хочу извлечь все имена подкатегорий и связанные с ними страницы.Я хочу, чтобы процесс сообщал результаты в формате BFS с глубиной 10. Существует ли какой-либо способ сделать это?

Я нашел следующий код из этого связанного сообщения:

from pprint import pprint
from urllib.parse import urljoin

from bs4 import BeautifulSoup
import requests


base_url = 'https://en.wikipedia.org/wiki/Category:Computer science'


def get_next_link(soup):
    return soup.find("a", text="next page")

def extract_links(soup):
    return [a['title'] for a in soup.select("#mw-pages li a")]


with requests.Session() as session:
    content = session.get(base_url).content
    soup = BeautifulSoup(content, 'lxml')

    links = extract_links(soup)
    next_link = get_next_link(soup)
    while next_link is not None:  # while there is a Next Page link
        url = urljoin(base_url, next_link['href'])
        content = session.get(url).content
        soup = BeautifulSoup(content, 'lxml')

        links += extract_links(soup)

        next_link = get_next_link(soup)

pprint(links)

1 Ответ

0 голосов
/ 06 октября 2018

Чтобы очистить подкатегории, вам придется использовать selenium для взаимодействия с выпадающими списками.Простой переход по второй категории ссылок приведет к появлению страниц, однако для поиска всех подкатегорий необходима рекурсия для правильной группировки данных.Приведенный ниже код использует простой вариант breadth-first search, чтобы определить, когда следует прекратить зацикливание объектов выпадающего переключателя, сгенерированных на каждой итерации цикла while:

from selenium import webdriver
import time
from bs4 import BeautifulSoup as soup 
def block_data(_d):
  return {_d.find('h3').text:[[i.a.attrs.get('title'), i.a.attrs.get('href')] for i in _d.find('ul').find_all('li')]}

def get_pages(source:str) -> dict:
  return [block_data(i) for i in soup(source, 'html.parser').find('div', {'id':'mw-pages'}).find_all('div', {'class':'mw-category-group'})]

d = webdriver.Chrome('/path/to/chromedriver')
d.get('https://en.wikipedia.org/wiki/Category:Computer_science')
all_pages = get_pages(d.page_source)
_seen_categories = []
def get_categories(source):
  return [[i['href'], i.text] for i in soup(source, 'html.parser').find_all('a', {'class':'CategoryTreeLabel'})]

def total_depth(c):
  return sum(1 if len(b) ==1 and not b[0] else sum([total_depth(i) for i in b]) for a, b in c.items())

def group_categories(source) -> dict:
  return {i.find('div', {'class':'CategoryTreeItem'}).a.text:(lambda x:None if not x else [group_categories(c) for c in x])(i.find_all('div', {'class':'CategoryTreeChildren'})) for i in source.find_all('div', {'class':'CategoryTreeSection'})}

while True:
  full_dict = group_categories(soup(d.page_source, 'html.parser'))
  flag = False
  for i in d.find_elements_by_class_name('CategoryTreeToggle'):
     try:
       if i.get_attribute('data-ct-title') not in _seen_categories:
          i.click()
          flag = True
          time.sleep(1)
     except:
       pass
     else:
       _seen_categories.append(i.get_attribute('data-ct-title'))
  if not flag:
     break

Вывод:

all_pages:

[{'\xa0': [['Computer science', '/wiki/Computer_science'], ['Glossary of computer science', '/wiki/Glossary_of_computer_science'], ['Outline of computer science', '/wiki/Outline_of_computer_science']]}, 
 {'B': [['Patrick Baudisch', '/wiki/Patrick_Baudisch'], ['Boolean', '/wiki/Boolean'], ['Business software', '/wiki/Business_software']]}, 
 {'C': [['Nigel A. L. Clarke', '/wiki/Nigel_A._L._Clarke'], ['CLEVER score', '/wiki/CLEVER_score'], ['Computational human modeling', '/wiki/Computational_human_modeling'], ['Computational social choice', '/wiki/Computational_social_choice'], ['Computer engineering', '/wiki/Computer_engineering'], ['Critical code studies', '/wiki/Critical_code_studies']]}, 
  {'I': [['Information and computer science', '/wiki/Information_and_computer_science'], ['Instance selection', '/wiki/Instance_selection'], ['Internet Research (journal)', '/wiki/Internet_Research_(journal)']]}, 
  {'J': [['Jaro–Winkler distance', '/wiki/Jaro%E2%80%93Winkler_distance'], ['User:JUehV/sandbox', '/wiki/User:JUehV/sandbox']]}, 
  {'K': [['Krauss matching wildcards algorithm', '/wiki/Krauss_matching_wildcards_algorithm']]}, 
  {'L': [['Lempel-Ziv complexity', '/wiki/Lempel-Ziv_complexity'], ['Literal (computer programming)', '/wiki/Literal_(computer_programming)']]}, 
  {'M': [['Machine learning in bioinformatics', '/wiki/Machine_learning_in_bioinformatics'], ['Matching wildcards', '/wiki/Matching_wildcards'], ['Sidney Michaelson', '/wiki/Sidney_Michaelson']]}, 
  {'N': [['Nuclear computation', '/wiki/Nuclear_computation']]}, {'O': [['OpenCV', '/wiki/OpenCV']]}, 
  {'P': [['Philosophy of computer science', '/wiki/Philosophy_of_computer_science'], ['Prefetching', '/wiki/Prefetching'], ['Programmer', '/wiki/Programmer']]}, 
  {'Q': [['Quaject', '/wiki/Quaject'], ['Quantum image processing', '/wiki/Quantum_image_processing']]}, 
  {'R': [['Reduction Operator', '/wiki/Reduction_Operator']]}, {'S': [['Social cloud computing', '/wiki/Social_cloud_computing'], ['Software', '/wiki/Software'], ['Computer science in sport', '/wiki/Computer_science_in_sport'], ['Supnick matrix', '/wiki/Supnick_matrix'], ['Symbolic execution', '/wiki/Symbolic_execution']]}, 
  {'T': [['Technology transfer in computer science', '/wiki/Technology_transfer_in_computer_science'], ['Trace Cache', '/wiki/Trace_Cache'], ['Transition (computer science)', '/wiki/Transition_(computer_science)']]}, 
  {'V': [['Viola–Jones object detection framework', '/wiki/Viola%E2%80%93Jones_object_detection_framework'], ['Virtual environment', '/wiki/Virtual_environment'], ['Visual computing', '/wiki/Visual_computing']]}, 
  {'W': [['Wiener connector', '/wiki/Wiener_connector']]}, 
  {'Z': [['Wojciech Zaremba', '/wiki/Wojciech_Zaremba']]}, 
  {'Ρ': [['Portal:Computer science', '/wiki/Portal:Computer_science']]}]

full_dict довольно большой, и из-за его размера я не могу опубликовать его полностью здесь, однако ниже приведена реализацияфункции для обхода структуры и выбора всех элементов на глубину до десяти:

def trim_data(d, depth, count):
   return {a:None if count == depth else [trim_data(i, depth, count+1) for i in b] for a, b in d.items()}

final_subcategories = trim_data(full_dict, 10, 0)

Редактировать: скрипт для удаления листьев из дерева:

def remove_empty_children(d):
  return {a:None if len(b) == 1 and not b[0] else 
     [remove_empty_children(i) for i in b if i] for a, b in d.items()}

При запуске выше:

c = {'Areas of computer science': [{'Algorithms and data structures': [{'Abstract data types': [{'Priority queues': [{'Heaps (data structures)': [{}]}, {}], 'Heaps (data structures)': [{}]}]}]}]}
d = remove_empty_children(c)

Вывод:

{'Areas of computer science': [{'Algorithms and data structures': [{'Abstract data types': [{'Priority queues': [{'Heaps (data structures)': None}], 'Heaps (data structures)': None}]}]}]}

Правка 2: выравнивание всей структуры:

def flatten_groups(d):
   for a, b in d.items():
     yield a
     if b is not None:
        for i in map(flatten_groups, b):
           yield from i


print(list(flatten_groups(remove_empty_children(c))))

Вывод:

['Areas of computer science', 'Algorithms and data structures', 'Abstract data types', 'Priority queues', 'Heaps (data structures)', 'Heaps (data structures)']

Правка3:

Чтобы получить доступ ко всем страницам для каждой подкатегории до определенного уровня, можно использовать оригинальную функцию get_pages и немного другую версию метода group_categories

def _group_categories(source) -> dict:
  return {i.find('div', {'class':'CategoryTreeItem'}).find('a')['href']:(lambda x:None if not x else [group_categories(c) for c in x])(i.find_all('div', {'class':'CategoryTreeChildren'})) for i in source.find_all('div', {'class':'CategoryTreeSection'})}
from collections import namedtuple
page = namedtuple('page', ['pages', 'children'])
def subcategory_pages(d, depth, current = 0):
  r = {} 
  for a, b in d.items():
     all_pages_listing = get_pages(requests.get(f'https://en.wikipedia.org{a}').text)
     print(f'page number for {a}: {len(all_pages_listing)}')
     r[a] = page(all_pages_listing, None if current==depth else [subcategory_pages(i, depth, current+1) for i in b])
  return r


print(subcategory_pages(full_dict, 2))

Обратите внимание, что для использования subcategory_pages необходимо использовать _group_categories вместо group_categories.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...