python BeautifulSoup断链检查器/网络爬虫

ma8fv8wu  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(120)

我正试图建立一个基于此操作的断开链接检查器:https://dev.to/arvindmehairjan/build-a-web-crawler-to-check-for-broken-links-with-python-beautifulsoup-39mg
然而,我在代码行上遇到了麻烦,因为当我运行程序时,我得到了这个错误消息:
File "/Users/Documents/brokenlinkchecker.py", line 26 print(f"Url: {link.get('href')} " + f"| Status Code: {response_code}") SyntaxError: invalid syntax
我被困在可能导致这个语法错误的原因上。有没有人能给我一些建议,我可以做些什么来使这个项目工作?
非常感谢!
代码如下:

# 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}")
nzk0hqpo

nzk0hqpo1#

您必须将额外的参数features="lxml"features="html.parser"传递给BeautifulSoup构造函数。

soup = BeautifulSoup(data,features="html.parser")

相关问题