나는 Scream으로 파이썬에서 웹 크롤러를 프로그래밍하고있다. 목적은 사전 결정된 시간 간격으로 웹 페이지의 변경 사항을 모니터링하는 것입니다. 웹 사이트에 로그인하면 스파이더가 X 분마다 웹 페이지를 요청하고 특정 데이터가 페이지에서 추출되어 텍스트 파일에 저장됩니다. 텍스트 파일은 거미가 닫히고 텍스트 파일의 줄이 연대순으로 정렬되지 않은 경우에만 쓰여지는 것으로 나타났습니다. 나는 무슨 일이 일어나고 있는지 파악할 수 없다. 아마 그것은 Scrapy 모듈을 작동시키는 특별한 방법일까요? 어떤 아이디어?scrapy spider : 연대순으로 출력
import scrapy
from scrapy.http import Request
from scrapy.http import FormRequest
from scraping_example.loginform import fill_login_form
from datetime import datetime
import time
class ExampleSpiderSpider(scrapy.Spider):
name = 'example_spider'
allowed_domains = ['example.com']
start_urls = ['http:/www.example.com/login']
login_user = 'edging780'
login_pass = ''
def parse(self, response):
(args, url, method) = fill_login_form(response.url,
response.body, self.login_user, self.login_pass)
return FormRequest(url, method=method, formdata=args,
callback=self.after_login)
def after_login(self, response):
for i in range(0,6):
request = Request('https://www.example.com/page_to_scrape', callback=self.get_table, dont_filter = True)
request.meta['dateTime'] = str(datetime.now())
request.meta['order'] = str(i)
yield request
time.sleep(600)
return
def get_table(self, response):
table = response.xpath('//table[@class="example_table"]/tbody/tr[not(contains(@class,"thead"))]')
Data=[]
for n_row in range(0,len(table)):
row = table[n_row]
Data.append(row.xpath('td[1]/text()').extract())
dictionary = {'Time': response.meta['dateTime'],
'Order': response.meta['order'],
'Data': Data}
with open('output.txt', 'a') as f:
f.write(str(dictionary) + '\n')
return
'after_login'을 생성자로 사용하면 의도 한대로 작동합니까? 나는 그것을 문서에서 찾을 수 없었다 : https://doc.scrapy.org/en/latest/topics/request-response.html, https://doc.scrapy.org/en/latest/topics/request-response .html # scrapy.http.FormRequest, https://doc.scrapy.org/en/latest/topics/request-response.html#topics-request-response-ref-request-callback-arguments, https : // doc .scrapy.org/ko/latest/topics/request-response.html # scrapy.http.FormRequest.from_response, https://stackoverflow.com/questions/5850755/using-scrapy-with-authenticated-logged-in-user - 세션, ... –
나는 당신의 질문을 이해하고 있는지 잘 모르겠다. 아마도 내가 불과 몇 달 전에 프로그래밍을 시작했기 때문일 것이다. 'after login' 메소드에서'yield'와'return'을 사용하는 것을 참고하면 온라인에서 발견 된 몇 가지 예제에서 가져 왔습니다. 'output.txt'의 줄이 연대순이 아닌 것을 제외하고는 코드가 작동합니다. – edding780
그런 예에 연결할 수 있습니까? ('yield'는 생성자 표현식을 생성하는데,'.next()'는 다음 값을 제공합니다. 이것은'return'과 다릅니다.) –