2013-07-12 43 views
6

Xin chào làm cách nào để trình thu thập thông tin của tôi hoạt động, tôi có thể đăng nhập nhưng không có gì xảy ra. Ngoài ra tôi đã đọc doc tài liệu và tôi thực sự không hiểu các quy tắc để sử dụng để cạo. Tại sao không có gì xảy ra sau khi "Đăng nhập thành công. Hãy bắt đầu thu thập dữ liệu!"Sử dụng thu thập thông tin thu thập dữ liệu để làm việc với phiên người dùng đã được xác thực (đã đăng nhập)

Tôi cũng đã có quy tắc này ở cuối tuyên bố khác của tôi nhưng xóa nó vì nó thậm chí không được gọi vì nó nằm trong khối khác của tôi. vì vậy tôi đã di chuyển nó ở đầu phương thức start_request() nhưng có lỗi nên tôi đã xóa các quy tắc của mình.

rules = (
       Rule(extractor,callback='parse_item',follow=True), 
       ) 

mã của tôi:

from scrapy.contrib.spiders.init import InitSpider 
from scrapy.http import Request, FormRequest 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.contrib.spiders import Rule 
from scrapy.contrib.spiders import CrawlSpider, Rule 

from scrapy.spider import BaseSpider 
from scrapy.selector import HtmlXPathSelector 

from linkedconv.items import LinkedconvItem 

class LinkedPySpider(CrawlSpider): 
    name = 'LinkedPy' 
    allowed_domains = ['linkedin.com'] 
    login_page = 'https://www.linkedin.com/uas/login' 
    # start_urls = ["http://www.linkedin.com/csearch/results?type=companies&keywords=&pplSearchOrigin=GLHD&pageKey=member-home&search=Search#facets=pplSearchOrigin%3DFCTD%26keywords%3D%26search%3DSubmit%26facet_CS%3DC%26facet_I%3D80%26openFacets%3DJO%252CN%252CCS%252CNFR%252CF%252CCCR%252CI"] 
    start_urls = ["http://www.linkedin.com/csearch/results"] 


    def start_requests(self): 
    yield Request(
    url=self.login_page, 
    callback=self.login, 
    dont_filter=True 
    ) 

    # def init_request(self): 
    #"""This function is called before crawling starts.""" 
    #  return Request(url=self.login_page, callback=self.login) 

    def login(self, response): 
    #"""Generate a login request.""" 
    return FormRequest.from_response(response, 
      formdata={'session_key': '[email protected]', 'session_password': 'mypassword'}, 
      callback=self.check_login_response) 

    def check_login_response(self, response): 
    #"""Check the response returned by a login request to see if we aresuccessfully logged in.""" 
    if "Sign Out" in response.body: 
     self.log("\n\n\nSuccessfully logged in. Let's start crawling!\n\n\n") 
     # Now the crawling can begin.. 
     self.log('Hi, this is an item page! %s' % response.url) 

     return 

    else: 
     self.log("\n\n\nFailed, Bad times :(\n\n\n") 
     # Something went wrong, we couldn't log in, so nothing happens. 


    def parse_item(self, response): 
    self.log("\n\n\n We got data! \n\n\n") 
    self.log('Hi, this is an item page! %s' % response.url) 
    hxs = HtmlXPathSelector(response) 
    sites = hxs.select('//ol[@id=\'result-set\']/li') 
    items = [] 
    for site in sites: 
     item = LinkedconvItem() 
     item['title'] = site.select('h2/a/text()').extract() 
     item['link'] = site.select('h2/a/@href').extract() 
     items.append(item) 
    return items  

myoutput

C:\Users\ye831c\Documents\Big Data\Scrapy\linkedconv>scrapy crawl LinkedPy 
2013-07-12 13:39:40-0500 [scrapy] INFO: Scrapy 0.16.5 started (bot: linkedconv) 
2013-07-12 13:39:40-0500 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetCon 
sole, CloseSpider, WebService, CoreStats, SpiderState 
2013-07-12 13:39:41-0500 [scrapy] DEBUG: Enabled downloader middlewares: HttpAut 
hMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, De 
faultHeadersMiddleware, RedirectMiddleware, CookiesMiddleware, HttpCompressionMi 
ddleware, ChunkedTransferMiddleware, DownloaderStats 
2013-07-12 13:39:41-0500 [scrapy] DEBUG: Enabled spider middlewares: HttpErrorMi 
ddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddle 
ware 
2013-07-12 13:39:41-0500 [scrapy] DEBUG: Enabled item pipelines: 
2013-07-12 13:39:41-0500 [LinkedPy] INFO: Spider opened 
2013-07-12 13:39:41-0500 [LinkedPy] INFO: Crawled 0 pages (at 0 pages/min), scra 
ped 0 items (at 0 items/min) 
2013-07-12 13:39:41-0500 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:602 
3 
2013-07-12 13:39:41-0500 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080 
2013-07-12 13:39:41-0500 [LinkedPy] DEBUG: Crawled (200) <GET https://www.linked 
in.com/uas/login> (referer: None) 
2013-07-12 13:39:42-0500 [LinkedPy] DEBUG: Redirecting (302) to <GET http://www. 
linkedin.com/nhome/> from <POST https://www.linkedin.com/uas/login-submit> 
2013-07-12 13:39:45-0500 [LinkedPy] DEBUG: Crawled (200) <GET http://www.linkedi 
n.com/nhome/> (referer: https://www.linkedin.com/uas/login) 
2013-07-12 13:39:45-0500 [LinkedPy] DEBUG: 


    Successfully logged in. Let's start crawling! 



2013-07-12 13:39:45-0500 [LinkedPy] DEBUG: Hi, this is an item page! http://www. 
linkedin.com/nhome/ 
2013-07-12 13:39:45-0500 [LinkedPy] INFO: Closing spider (finished) 
2013-07-12 13:39:45-0500 [LinkedPy] INFO: Dumping Scrapy stats: 
    {'downloader/request_bytes': 1670, 
    'downloader/request_count': 3, 
    'downloader/request_method_count/GET': 2, 
    'downloader/request_method_count/POST': 1, 
    'downloader/response_bytes': 65218, 
    'downloader/response_count': 3, 
    'downloader/response_status_count/200': 2, 
    'downloader/response_status_count/302': 1, 
    'finish_reason': 'finished', 
    'finish_time': datetime.datetime(2013, 7, 12, 18, 39, 45, 136000), 
    'log_count/DEBUG': 11, 
    'log_count/INFO': 4, 
    'request_depth_max': 1, 
    'response_received_count': 2, 
    'scheduler/dequeued': 3, 
    'scheduler/dequeued/memory': 3, 
    'scheduler/enqueued': 3, 
    'scheduler/enqueued/memory': 3, 
    'start_time': datetime.datetime(2013, 7, 12, 18, 39, 41, 50000)} 
2013-07-12 13:39:45-0500 [LinkedPy] INFO: Spider closed (finished) 

Trả lời

5

Ngay bây giờ, bò kết thúc bằng check_login_response() vì Scrapy đã không được cho biết phải làm gì hơn.

  • yêu cầu 1 đến trang đăng nhập sử dụng start_requests(): OK
  • yêu cầu 2 thành POST thông tin đăng nhập: OK
  • mà đáp ứng được phân tách với check_login_response ... và đó là nó

Trên thực tế check_login_response() không trả lại gì cả. Để giữ cho bò đi, bạn cần phải trả lại Request trường (mà nói Scrapy gì trang để tìm nạp tiếp theo, hãy xem tài liệu Scrapy trên callbacks Nhện)

Vì vậy, bên trong check_login_response(), bạn cần phải trả lại một trường hợp Request để trang bắt đầu chứa các liên kết bạn muốn thu thập thông tin tiếp theo, có thể là một số URL bạn đã xác định trong start_urls.

def check_login_response(self, response): 
     #"""Check the response returned by a login request to see if we aresuccessfully logged in.""" 
     if "Sign Out" in response.body: 
      self.log("\n\n\nSuccessfully logged in. Let's start crawling!\n\n\n") 
      # Now the crawling can begin.. 
      return Request(url='http://linkedin.com/page/containing/links') 

Theo mặc định, nếu bạn không đặt một callback cho Request của bạn, nhện gọi phương thức parse() của nó (http://doc.scrapy.org/en/latest/topics/spiders.html#scrapy.spider.BaseSpider.parse).

Trong trường hợp của bạn, nó sẽ tự động gọi CrawlSpider phương pháp tích hợp parse() cho bạn, áp dụng các số Rule mà bạn đã xác định để nhận các trang tiếp theo.

Bạn phải xác định các quy tắc CrawlSpider trong thuộc tính rules của lớp nhện của mình, giống như bạn đã làm cho name, allowed_domain v.v., cùng cấp.

http://doc.scrapy.org/en/latest/topics/spiders.html#crawlspider-example cung cấp các quy tắc mẫu. Ý tưởng chính là bạn nói với trình trích xuất loại URL tuyệt đối mà bạn quan tâm trong trang, sử dụng (các) cụm từ thông dụng trong allow. Nếu bạn không đặt allow trong SgmlLinkExtractor, nó sẽ khớp với tất cả các liên kết.

Và mỗi Quy tắc phải có gọi lại để sử dụng cho các liên kết này, trong trường hợp của bạn là parse_item().

Chúc may mắn với việc phân tích các trang LinkedIn, tôi nghĩ rất nhiều nội dung trong các trang được tạo thông qua Javascript và có thể không nằm trong nội dung HTML được tìm nạp bởi Scrapy.

+0

Cảm ơn Paul điều này rất hữu ích – Gio

+0

Bạn được chào đón. –

Các vấn đề liên quan