Find years in string like (90-)

Viewed 56

I don't know how to create IF with like two numbers and with "-" for example

IQ 09-

70-serie

70 84-89

80-serie

80 90-97

90-serie

90 96-99

90 99-02

but without

90-serie 80-serie

I created function like:

import time
from termcolor import colored
start_time = time.time()
url="https://www.frontlykter.no/"
def scrape(page):
    url = page
    soup = BeautifulSoup(requests.get(url).content, 'html.parser')
    count=0
    urls=[]

    menu=soup.select('.ls-categories-ajax')
    for a in menu[0].find_all('a', href=True):
        if "-" in a.text:
            object = {
                "URL": a['href'],
                "Name":a.text
            }

            # print(a['href'])
            print(a.text)
            count += 1
            urls.append(object)
    print(colored("Liczba linkow: ", "yellow"), count)
    return urls

I need to modify my "if" What should I do?

Summarizing. I am looking for the last category links in the menu. That is, those with vintage car models. I don't know if it's the best idea with this "if".

I just don't want to read links like:

Citroen-> AX Only those that are in AX (they have years in the name)

Edit or maybe find all Third UL or Last child UL?

1 Answers

I propose you this behavior :

So you want only model with this scheme : "NUM-NUM" or "NUM-". That what you can code in your if function :

Check if '-' is the last character, so you accept it. Or, you have to check if what is after '-' is a digit.

To do this, you can use find which will return you the position of your find research. If what you are searching for does not exist, find return -1.

import time
from termcolor import colored
from bs4 import BeautifulSoup
import requests

start_time = time.time()
url="https://www.frontlykter.no/"

def scrape(page):
    
    def add_object(a, urls):
        object = {
                "URL": a['href'],
                "Name":a.text
        }
        urls.append(object)
        return urls
            
    url = page
    soup = BeautifulSoup(requests.get(url).content, 'html.parser')
    urls=[]

    menu=soup.select('.ls-categories-ajax')
    
    for a in menu[0].find_all('a', href=True):
        
        pos = a.text.find('-')
        length = len(a.text)
        
        # So there is a '-'
        if pos != -1 :
            
            # '-' is at last position so OK
            if pos == length-1 :
                print(a.text)
                urls = add_object(a, urls)
            
            # '-' is not last position
            if pos < length-1 :
                # check if what is after '-' is a number and not a caracter
                if a.text[pos+1].isdigit() or a.text[pos+1]==' ':
                    print(a.text)
                    urls = add_object(a, urls)
                
    count = len(urls)
            
    print(colored("Liczba linkow: ", "yellow"), count)
    return urls

scrape(url)
Related