2

I am trying to build a broken link checker based on this How-to: https://dev.to/arvindmehairjan/build-a-web-crawler-to-check-for-broken-links-with-python-beautifulsoup-39mg

However, I'm having trouble with the line of code, since when I run the program, I get this error message:

File "/Users/Documents/brokenlinkchecker.py", line 26 print(f"Url: {link.get('href')} " + f"| Status Code: {response_code}") SyntaxError: invalid syntax

I'm stuck on what might be causing this syntax error. Would anyone have advice on what I could do to make this program work?

Thank you very much!

Here is the code:

# Import libraries
from bs4 import BeautifulSoup, SoupStrainer
import requests

# Prompt user to enter the URL
url = input("Enter your url: ")

# Make a request to get the URL
page = requests.get(url)

# Get the response code of given URL
response_code = str(page.status_code)

# Display the text of the URL in str
data = page.text

# Use BeautifulSoup to use the built-in methods
soup = BeautifulSoup(data)

# Iterate over all links on the given URL with the response code next to it
for link in soup.find_all('a'):
    print(f"Url: {link.get('href')} " + f"| Status Code: {response_code}")
Pfalbaum
  • 586
  • 3
  • 10
  • 26
K C
  • 21
  • 1

1 Answers1

0

You have to pass the additional argument features="lxml" or features="html.parser" to the BeautifulSoup constructor.

soup = BeautifulSoup(data,features="html.parser")
shamnad sherief
  • 552
  • 5
  • 17