python刮链接关键字

python刮链接关键字,python,scrape,Python,Scrape,我是python新手,需要帮助删除带有某个关键字的所有链接。问题是我遇到了以下错误: 如果链接[“href”]中的“air max”: ^ 缩进错误:应为缩进块 这是我的密码 import requests import time from bs4 import BeautifulSoup headers = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8","X-Requested-With": "XML

我是python新手,需要帮助删除带有某个关键字的所有链接。问题是我遇到了以下错误:

如果链接[“href”]中的“air max”: ^ 缩进错误:应为缩进块

这是我的密码

import requests
import time
from bs4 import BeautifulSoup

headers = {"Content-Type": "application/x-www-form-urlencoded; 
charset=UTF-8","X-Requested-With": "XMLHttpRequest","User-Agent": 
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, 
like Gecko) Chrome/56.0.2924.87 Safari/537.36"}

for i in range(0,4):   
url = "https://www.aw-lab.com/shop/uomo/scarpe?p={}".format(i)
r = requests.get(url)
soup = BeautifulSoup(r.content, "html.parser")

all_links = soup.find_all("a")
for link in all_links:
if link.has_key('href'):
if "air-max" in link["href"]:
    print(link["href"])

链接之后需要另一个缩进级别。has_key('href'):
。而且,要始终如一;始终使用空格(首选)或始终使用选项卡。这可能并不总是正确的,但一般来说,如果一行末尾有冒号
,那么下一行应该进一步缩进一级

for i in range(0,4):   
    url = "https://www.aw-lab.com/shop/uomo/scarpe?p={}".format(i)
    r = requests.get(url)
    soup = BeautifulSoup(r.content, "html.parser")

    all_links = soup.find_all("a")
    for link in all_links:
        if link.has_key('href'):
            if "air-max" in link["href"]:
                print(link["href"])

请使用spyder IDE或jupyter笔记本等IDE进行开发

import requests
import time
from bs4 import BeautifulSoup

headers = {"Content-Type": "application/x-www-form-urlencoded; 
charset=UTF-8","X-Requested-With": "XMLHttpRequest","User-Agent": 
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, 
like Gecko) Chrome/56.0.2924.87 Safari/537.36"}

for i in range(0,4):
    url = "https://www.aw-lab.com/shop/uomo/scarpe?p={}".format(i)
    r = requests.get(url)
    soup = BeautifulSoup(r.content, "html.parser")

all_links = soup.find_all("a")
for link in all_links:
    if link.has_key('href'):
        if "air-max" in link["href"]:
            print(link["href"])

你必须缩进你的代码。确保遵循样式,检查是否在正确的位置到处都有空格或制表符。