我希望你能就我的問題給我一些提示。
我正在嘗試從txt源獲取有序數據。代碼運行良好,直到我從txt源打印數據,所以它讀取它。但有一次我啟動了一個循環,從txt文件中讀取每一行,然后我“print(origdato)”檢查它是否工作正常,但它沒有。
也許是循環,也許是斯派德的要求,我真的不知道。
你能幫幫我嗎?
代碼如下:
# packages
import scrapy
from scrapy.crawler import CrawlerProcess
from scrapy.selector import Selector
import json
import datetime
# scraper class
class myfile(scrapy.Spider):
# scraper name
name= 'whatever'
base_url = 'https://www.whatever.com/'
headers = {'...'
}
custom_settings = {
'CONCURRENT_REQUEST_PER_DOMAIN': 1,
'DOWNLOAD_DELAY': 1,
}
current_page = 2
origdatos= []
def __init__(self):
content = ''
with open('origdatos.txt', 'r') as f:
for line in f.read():
content += line
# parse content
self.origdatos= content.split('\n')
# print(self.origdatos) // Till heree works fine
# crawler
def start_requests(self):
self.current_page = 2
# loop over datos
for origdato in self.origdatos:
print(origdato) #In this print Python does not show me data, so it appears the loop does not work properly, maybe
#driver
if __name__ == '__main__':
# run scraper
process = CrawlerProcess()
process.crawl(myfile)
process.start()
可能這是代碼的格式問題。如果它的格式如問題所示,那就是。嘗試取消代碼中
start_requests
方法的標識,看看它是否修復了問題。以下措施也應起作用:
但是,這仍然會在執行結束時產生錯誤,因為
start_requests
應該返回一個iterable。