Вы пытались искать свое имя в Google? Или чье-то имя? Какое самое распространенное слово связано с вашим именем? В этом руководстве мы узнаем:)
Требования:
- Питон
- Интернет-соединение:)
У нас есть 2 раздела в этом руководстве. Scraping
а также Визуализация
Установка Scrapy:
pip install scrapy
Первая попытка
Давайте создадим наш проект.
scrapy startproject google cd google
Давайте попробуем поиск вашего имени в Google. Я собираюсь полагать, что ваше зовут Сукиджан
Отказ
Получить весь текст
Давайте постараемся получить весь текст с URL. Я попробую с этим URL https://www.aljazeera.com/news/2019/06/iseali-Forces-settlers-enter-al-aqsa-mosque-compount-190602065712978.html
(Одна из первых новостей у меня сегодня утром)
Создать файл Google/Google/Spiders/test.py
import scrapy class TestSpider(scrapy.Spider): name = 'test' # Tell scrapy, we want to start crawing with these pages start_urls = [ 'https://www.aljazeera.com/news/2019/06/israeli-forces-settlers-enter-al-aqsa-mosque-compound-190602065712978.html'] # What we're going to do after the page is resolved def parse(self, response): # We yield a dict of this yield { # response.css is css style selector. # ::text means we want to to get the text. If we don't put ::text # We'll getIsraeli forces and settlers ... # When we add text we got 'Israeli forces and settlers ...' 'title': response.css('title::text').get(), # Same thing with `p`, We're selecting the text of element p # But notice that we use `getall` instead of `get` # `get` will select only one element. # While `getall` Will select all element, and return list 'text': response.css('p::text').getall() }
Теперь попробуйте запустить Тест Scrapy Crawl.
.Вы будете Смотрите в результате в командной строке.
Давайте попробуем написать результат в файл с Scrapy Crawl Test -o результат .json
Теперь вы можете увидеть результат в формате JSON в Результат
Получить ссылки из Google
Давайте попробуем ползти много URL из Google
Прежде всего. Перейти к settings.py
и убедитесь
... ROBOTSTXT_OBEY = False ...
import scrapy import re def is_google(a): url = a.attrib['href'] return bool(re.search("/search", url)) class TestSpider(scrapy.Spider): name = "test" # How to get the start url # search `sukijan` on Google. And then go to second page, and then click first page result. You'll see that the url now includes `start` parameter. We'll use that for pagination # Also pay attention to parameter `q=sukijan`. That's our keyword # We use `% start for start in range(0, 100, 10)`. To generate urls that we want to crawl # So it will generate a list of urls like so # [q=sukijan start =0, q=sukijan start=10, q=sukijan start=20, q=sukijan, start=30... until 100] start_urls = [ 'https://www.google.com/search?q=sukijan&safe=strict&ei=sLTvXL_KH4zfz7sPpZOBuA4&start=%s&sa=N&ved=0ahUKEwi_4un2icPiAhWM73MBHaVJAOc4ChDx0wMIjQE&cshid=1559213273144254&biw=1680&bih=916' % start for start in range(0, 100, 10) ] def parse(self, response): # What is `jfp3ef a` # If you do `scrapy shell https://www.google.com/search?q=Sukijan&oq=sukijan&aqs=chrome.0.69i59j0l5.1739j0j7&sourceid=chrome&ie=UTF-8#ip=1` # And then do `view(response)`. It will open up a browser # From there do inspect element, locate a link, And you'll find that most of the links fall under `.jfp3ef` class for href in response.css('.jfp3ef a'): # We want to open url these links. But we don't want to open Google's url # For example url `More images for Sukijan`, etc. if not is_google(href): # This basically means 'Hey scrapy` follow this url. # When you find it run parse_text function on it. yield response.follow(href, self.parse_text) def parse_text(self, response): # get_text takes the response # And then it will takes all text whether it's intag,
tag,
tag etc # But it will leave everything inside
Оригинал: “https://dev.to/muhajirdev/scraping-from-google-visualizing-most-common-word-with-wordcloud-4mko”