2017-10-16 134 views
-3

我是python的新手,我需要幫助才能抓取某個關鍵字的所有鏈接。問題是,我發現了以下錯誤:python scrape links關鍵字

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

這裏是我的代碼

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

請使用像spyder IDE或jupyter notebook開發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

你需要link.has_key('href'):陸續縮進級別。另外,要一致;始終使用空格(首選)或始終使用製表符。這可能並非總是如此,但是,一般來說,如果在行尾有COLON :,則下一行應該進一步縮進一級。

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