2017-10-16 7 views
-3

私はPythonの初心者です。特定のキーワードですべてのリンクを掻き集めるのに助けが必要です。問題は、私は次のエラーを取得していますということです。ここでpython scrape linksキーワード

if "air-max" in link["href"]: ^ IndentationError: expected an indented block.

はあなたがlink.has_key('href'):後に別のインデントレベルを必要とする私のコード

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"]) 
+2

コードをインデントする必要があります。あなたが正しい場所にどこにでもスペースやタブがあるかどうかを確認して、スタイルに従ってください。 – lll

答えて

-1

開発のために、スパイダーIDEまたはジュピターノートのような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"]) 
0

です。また、一貫していること。必ずスペースを使用するか(推奨)、常にタブを使用してください。これは必ずしも真実であるとは限りませんが、一般的には、行末にCOLON :がある場合、次の行は1レベル下にインデントされる必要があります。

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"])