Как парсить сайт используя Python 3? Какие библиотеки нужны?

Нужна страница обычная википидии.


Ответы (3 шт):

Автор решения: Alex Zaharchuk

Библиотеки: requests, BeautifulSoup. Как парсить довольно подробно и на русском написано здесь

→ Ссылка
Автор решения: MarianD

Достаточный для вашей цели модуль Beautiful Soup:

pip install beautifulsoup4

В вашей программе затем сделайте импорт:

from bs4 import BeautifulSoup

чтобы использовать его и можете работать.

→ Ссылка
Автор решения: Victor VosMottor thanks Monica

pip3 install wikipedia

Example:

>>> import wikipedia
>>> print wikipedia.summary("Wikipedia")
# Wikipedia (/ˌwɪkɨˈpiːdiə/ or /ˌwɪkiˈpiːdiə/ WIK-i-PEE-dee-ə) is a collaboratively edited, multilingual, free Internet encyclopedia supported by the non-profit Wikimedia Foundation...

>>> wikipedia.search("Barack")
# [u'Barak (given name)', u'Barack Obama', u'Barack (brandy)', u'Presidency of Barack Obama', u'Family of Barack Obama', u'First inauguration of Barack Obama', u'Barack Obama presidential campaign, 2008', u'Barack Obama, Sr.', u'Barack Obama citizenship conspiracy theories', u'Presidential transition of Barack Obama']

>>> ny = wikipedia.page("New York")
>>> ny.title
# u'New York'
>>> ny.url
# u'http://en.wikipedia.org/wiki/New_York'
>>> ny.content
# u'New York is a state in the Northeastern region of the United States. New York is the 27th-most exten'...
>>> ny.links[0]
# u'1790 United States Census'

>>> wikipedia.set_lang("fr")
>>> wikipedia.summary("Facebook", sentences=1)
# Facebook est un service de réseautage social en ligne sur Internet permettant d'y publier des informations (photographies, liens, textes, etc.) en contrôlant leur visibilité par différentes catégories de personnes.


pip install Wikipedia-API

Example:

def print_categorymembers(categorymembers, level=0, max_level=1):
        for c in categorymembers.values():
            print("%s: %s (ns: %d)" % ("*" * (level + 1), c.title, c.ns))
            if c.ns == wikipediaapi.Namespace.CATEGORY and level < max_level:
                print_categorymembers(c.categorymembers, level=level + 1, max_level=max_level)


cat = wiki_wiki.page("Category:Physics")
print("Category members: Category:Physics")
print_categorymembers(cat.categorymembers)

# Category members: Category:Physics
# * Statistical mechanics (ns: 0)
# * Category:Physical quantities (ns: 14)
# ** Refractive index (ns: 0)
# ** Vapor quality (ns: 0)
# ** Electric susceptibility (ns: 0)
# ** Specific weight (ns: 0)
# ** Category:Viscosity (ns: 14)
# *** Brookfield Engineering (ns: 0)


pip3 install bs4

pip3 install requests

import requests
from bs4 import BeautifulSoup as Soup


url = 'https://ru.wikipedia.org/wiki/Python'


source = requests.get(url)
main_text = source.text
soup = Soup(main_text)

data = soup.find('div', {'class': 'mw-parser-output'})
print(data.p)
→ Ссылка