2015-11-21 181 views
0

这里是我的代码现在:的Python Crawling-类型错误:“诠释”对象不是可迭代

front_deeplink = ("http://www.jsox.de") 
Spider = 20 
Region = 7236 


def trade_spider(max_pages): 
for reg in Region: 
    page = 0 
    while page <= max_pages: 
     page += 1 
     r = requests.get("http://www.jsox.de/affiliatesearch.aspx?&regionid=" + str(reg) + "&pid=" + str(page)) 
     soup = BeautifulSoup(r.content) 

     g_data = soup.find_all("div", {"class": "gridHeadOuter productInfoOuter"}) 

     for item in g_data: 
      Header = item.find_all("div", {"class": "offerInto"}) 
      Header_final = (Header[0].contents[0].text.strip()) 
      price = item.find_all("strong", {"class": "priceBig priceBlock"}) 
      Price_final = (price[0].text.strip()) 
      Deeplink = item.find_all("a") 
      for t in set(t.get("href") for t in Deeplink):   
       Deeplink_final = (str(front_deeplink) + t) 

      print("Header: " + Header_final + " | " + "Price: " + Price_final[:-1] + " | " + "Deeplink: " + Deeplink_final) 

trade_spider(int(Spider)) 

不过,我不断收到错误消息:

for reg in Region: 
TypeError: 'int' object is not iterable 
当我尝试

和运行这个。

我在做什么错?

任何人都可以帮助我吗?任何反馈意见。

+0

你已经定义了'Region = 7236',它是一个'int',所以就像错误所说的那样,你不能迭代它。 – agold

+0

你想做什么?你应该怎么处理数字7236? –

+0

7236简化为一个区域的数字,例如罗马 –

回答

1

使用范围()xrange(),它接收一个int并产生一个迭代。

3

在你的代码有这样的声明:

Region = 7236 

然后,你试试这样做:

for reg in Region: 

显然,Region只是一个数字,你不能迭代就像你会说,遍历一个列表。使用for来遍历列表或其他可迭代对象 - 数字根本无法迭代。

相关问题