Очистка веб-страниц: не удается очистить текст и ссылки для заданного элемента div, класса и пропустить тег span - PullRequest
0 голосов
/ 22 апреля 2020

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

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

Мой код:

import requests
from bs4 import BeautifulSoup
import psycopg2
import time


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


def getTitleURL():
    url = 'http://sandesh.com/'
    response = checkResponse(url)

    if response is not None:
        html = BeautifulSoup(response, 'html.parser')

    for values in html.find_all('div', class_='d-top-news-latest'):
        headline = values.find(class_='d-s-NSG-regular').text
        url = values.find(class_='d-s-NSG-regular').['href']
        print(headline + "->" + url)


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

Вывод:

Getting the list of names....
Corona live
મેડિકલ સ્ટાફ પર હુમલા અંગે અમિત શાહે ડોક્ટર્સ સાથે કરી ચર્ચા, સુરક્ષાની ખાતરી આપતા કરી અપીલ





Ahmedabad
ગુજરાતમાં કૂદકેને ભૂસકે વધ્યો કોરોના વાયરસનો કહેર, આજે નવા 94 કેસ નોંધાયા, જાણો કયા- કેટલા કેસ નોંધાયા





Corona live
જીવન અને મોત વચ્ચે સંઘર્ષ કરી રહ્યો છે દુનિયાનો સૌથી મોટો તાનાશાહ કિમ જોંગ! ટ્રમ્પે કહી આ વાત





Ahmedabad
અમદાવાદમાં નર્સિંગ સ્ટાફનો ગુસ્સો ફૂટ્યો, ‘અમારું કોઈ સાંભળતું નથી, અમારો કોરોના ટેસ્ટ જલદી કરાવો’





Business
ભારતીય ટેલિકોમ જગતમાં સૌથી મોટી ડીલ, ફેસબુક બની જિયોની સૌથી મોટી શેરહોલ્ડર


->http://sandesh.com/amit-shah-talk-with-ima-and-doctors-through-video-conference-on-attack/
... done.

Я хочу пропустить текст внутри тега, а также могу получить только 1 href. Также заголовок - это список. как мне получить каждый заголовок и URL.

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

enter image description here

Ответы [ 2 ]

1 голос
/ 22 апреля 2020

Во-первых, в for values in html.find_all('div', class_='d-top-news-latest') вам не нужно использовать for, потому что в DOM просто есть один класс d-top-news=latest.

Во-вторых, чтобы получить название, вы можете использовать select('span') из-за Ваш заголовок в теге span.

В-третьих, вы знали, что заголовок - это список, поэтому вам нужно использовать for для получения каждого заголовка и URL.

values = html.find('div', class_='d-top-news-latest')
for i in values.find_all('a', href = True):
    print(i.select('span'))
    print(i['href'])

ВЫХОД

Getting the list of names....
[<span>
Corona live
</span>]
http://sandesh.com/maharashtra-home-minister-anil-deshmukh-issue-convicts-list-of- 
palghar-case/
[<span>
Corona live
</span>]
http://sandesh.com/two-doctors-turn-black-after-treatment-of-coronavirus-in-china/
[<span>
Corona live
</span>]
http://sandesh.com/bihar-asi-gobind-singh-suspended-for-holding-home-guard-jawans- 
after-stopping-officers-car-asi/
[<span>
Ahmedabad
</span>]
http://sandesh.com/jayanti-ravi-surprise-statement-sparks-outcry-big-decision-taken- 
despite-more-patients-in-gujarat/
[<span>
Corona live
</span>]
http://sandesh.com/amit-shah-talk-with-ima-and-doctors-through-video-conference-on- 
attack/
... done.
0 голосов
/ 22 апреля 2020

для удаления части "span":

values = html.find('div', class_='d-top-news-latest')
for i in values.find_all('a', href=True):
    i.span.decompose()
    print(i.text)
    print(i['href'])

Вывод:

Getting the list of names....

ગુજરાતમાં કોરોનાનો કહેરઃ રાજ્યમાં આજે કોરોનાના 135 નવા કેસ, વધુ 8 લોકોનાં મોત

http://sandesh.com/gujarat-corona-update-206-new-cases-and-18-deaths/

ચીનના વૈજ્ઞાનિકોએ જ ખોલી જીનપિંગની પોલ, કોરોના વાયરસને લઈને કર્યો સનસની ખુલાસો

http://sandesh.com/chinese-scientists-claim-over-corona-virus/

શું લોકડાઉન ફરી વધારાશે? PM મોદી 27મીએ ફરી એકવાર તમામ CM સાથે કરશે ચર્ચા

http://sandesh.com/pm-modi-to-hold-video-conference-with-cms-on-april-27-lockdown- 
extension/

કોરોના વાયરસને લઈ મોટી ભવિષ્યવાણી, દુનિયાના 30 દેશો પર ઉભુ થશે ભયંકર સંકટ

http://sandesh.com/after-corona-attack-now-hunger-will-kill-many-people-in-the-world/

દેશમાં 24 કલાકમાં 1,486 કોરોનાનાં નવા કેસ, પરંતુ મળ્યા સૌથી મોટા રાહતનાં સમાચાર

http://sandesh.com/recovery-rate-increased-in-corona-patients-says-health-ministry/
... done.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...