[Python] Beautifulsoap - reading multiply pages breaks after random valid reads

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm reading some data about books title etc from number of pages.
    Python 3.10.13 Breautifulsoap 4.12.3

    Code:

    def scrapSite(URL):

    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0"}


    page = requests.get(URL,headers=headers)
    soup = BeautifulSoup(page.content, "html.parser")
    # Extract title
    title_tag = soup.find('h1', class_='book__title')
    title = title_tag.text.strip() if title_tag else 'Title not found'
    return(title)


    then I'm calling out the scrapSite functiona like this:

    counter = 0
    for data in books_links:
    counter +=1
    url = data[0]
    print(url + ' '+ str(counter))
    values = scrapSite2(url)
    if values == None:
    print(f'Values niestety none dla: {url} numer {counter}')
    # books_data.append(values)
    break


    book_links is just a table with URLs ['https://excample.com/booknumber12/','https://excample.com/booknumber34/' ...]

    Problem statement:
    After random number of pages read successfully the scrapSite function returns none. Its an issue as I know the title is there as sometimes say it passes site 70 failing on say 81 but sometimes it fails on 70 already. The fail page number is usually higher then 63.
    I've tried:
    using insetead of htmp.parser, xml one or html5lib but still the same issue. Like there is cache being created even thought I'm calling new beautigulsoap object each time.
    When I list soap parsed object I've notice that for the failed page it is always like

    <header>
    ....
    </header>
    <body>
    <some tags> <image aoanAIUANsLK OAIRNGAsrsgsK AORINNBArsdsfsIABA


    and small and capital letters goes on and on where as for correct reads obviously the soap parsed object is clean.

    Continue reading...

Compartilhe esta Página