我是Python新手,我刚开始学习Scrapy。在
我在spider文件中的代码如下:
from openbl.items import OpenblItem
import scrapy
import time
class OpenblSpider(scrapy.Spider):
name='openbl'
start_url=['http://www.openbl.org/lists/base_1days.txt']
def parse(self, response):
#get the content within 'pre', select the 1st element to get the content string.
#split the space of the content
content=response.xpath('/pre/text').extract()[0].split()
# This for loop is used to get the num of element in list content
# after which the elements of the list are the IPs we desire.
for i in range(0,len(content)):
if content[i]=='ip':
i+=1
break
else:
pass
# construct a new list content_data for putting IPs in.
content_data=[]
# This for loop put useful data(IPs) into the new list above.
for x in range(i,len(content)):
content_data.append(content(i))
for cont in content_data:
item=OpenblItem()
item['name']=cont
item['date']=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
item['type']='other'
yield item
我正在抓取的网站是:http://www.openbl.org/lists/base_1days.txt 我想从这个网站得到IP作为项目['name']
如果有人能回答我的问题,我将不胜感激。在
现在出现了一些错误。。在
^{pr2}$
首先,这个代码有几个问题
start_urls
否则我认为它不会抓取任何东西您得到的错误是因为它是一个纯文本文件响应.正文是纯文本。没有标签。所以你得到了一个索引越界异常。您可以简单地将其处理为纯文本,并通过正则表达式提取信息,通过
\n
s等进行拆分。在另外,不要使用循环变量,我喜欢这样。这感觉不对。如果你想找到列表中某物第一次出现的索引,有
index()
函数。或者 What is the best way to get the first item from an iterable matching a condition?相关问题 更多 >
编程相关推荐