-3

I am trying to get input from the user, more specifically a link. The 'https://' part is automatic, but I want the user to be asked again if he doesn't put 'www' in the beginning of the link.

This is the code by now:

import requests
from bs4 import BeautifulSoup

linkraw = input("Site-ul: ")

while linkraw[0] != "www":
    print("Pune linkul incepand cu 'www' 2")
    linkraw = input("Site-ul: ")

link = f'https://{linkraw}'

httprequest = requests.get(link)

if httprequest.status_code == 200:
    print("Valid")
if httprequest.status_code != 200:
    print("Invalid")

src = httprequest.content
soup = BeautifulSoup(src,'lxml')

print(soup.find_all('a'))

2 Answers2

2

You want to check whether the input begins with "www".

if linkraw.startswith("www"):

Cu placere ... :-)


Your commented case is incorrect:

>>> linkraw = "w.google.com"
>>> linkraw.startswith("www")
False
Prune
  • 76,765
  • 14
  • 60
  • 81
1

You can check if the first 3 letters of the value are the ones you are looking for.

if linkraw[0:3] == "www"
dharmey
  • 85
  • 2
  • 8