Веб-очистка: не в состоянии очистить текст и ссылки для данного класса, класса - PullRequest
0 голосов
/ 21 апреля 2020

Пытается получить текст и href для главных новостей, но не может их проанализировать.

веб-сайт: новостной сайт

import requests
from bs4 import BeautifulSoup


def checkResponse(url):
    response = requests.get(url)
    if response.status_code == 200:
        return response.content
    else:
        return None


def getTitleURL():
    url = 'https://www.gujaratsamachar.com/'
    response = checkResponse(url)
    if response is not None:
        html = BeautifulSoup(response, 'html.parser')

    for values in html.find_all('div', class_='main-news'):
        print(values.a.href)


if __name__ == '__main__':
    print('Getting the list of names....')
    names = getTitleURL()
    print('... done.\n')

Вывод пусто

Попытка вычистить деталь красным цветом:

enter image description here

Элементы выглядят так:

enter image description here

1 Ответ

1 голос
/ 21 апреля 2020
import requests

data = ["heading", "categorySlug", "articleUrl"]



def main(url):
    r = requests.get(url).json()
    for item in r['data']:
        goal = [item[d] for d in data]
        print(goal[0], f"{url[:31]}/news/{'/'.join(goal[1:])}")


main("https://www.gujaratsamachar.com/api/stories/5993f2835b03ab694185ad25?type=top-stories")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...