Scrapy to login and then grab data from Weibo

206 Views Asked by At

I am still trying to use Scrapy to collect data from pages on Weibo which need to be logged in to access.

I now understand that I need to use Scrapy FormRequests to get the login cookie. I have updated my Spider to try to make it do this, but it still isn't working.

Can anybody tell me what I am doing wrong?

import scrapy

class LoginSpider(scrapy.Spider):
    name = 'WB'

    def start_requests(self):
        return [
            scrapy.Request("https://www.weibo.com/u/2247704362/home?wvr=5&lf=reg", callback=self.parse_item)
        ]

    def parse_item(self, response):
        return scrapy.FormRequest.from_response(response, formdata={'user': 'user', 'pass': 'pass'}, callback=self.parse)

    def parse(self, response):
        print(response.body)

When I run this spider. Scrapy redirects from the URL under start_requests, and then returns the following error:

ValueError: No element found in <200 https://passport.weibo.com/visitor/visitor?entry=miniblog&a=enter&url=https%3A%2F%2Fweibo.com%2Fu%2F2247704362%2Fhome%3Fwvr%3D5%26lf%3Dreg&domain=.weibo.com&ua=php-sso_sdk_client-0.6.28&_rand=1585243156.3952>

Does that mean I need to get the spider to look for something other than Form data in the original page. How do I tell it to look for the cookie?

I have also tried a spider like this below based on this post.

import scrapy

class LoginSpider(scrapy.Spider):
    name = 'WB'

    login_url = "https://www.weibo.com/overseas"
    test_url = 'https://www.weibo.com/u/2247704362/'

    def start_requests(self):
        yield scrapy.Request(url=self.login_url, callback=self.parse_login)

    def parse_login(self, response):
        return scrapy.FormRequest.from_response(response, formid="W_login_form", formdata={"loginname": "XXXXX", "password": "XXXXX"}, callback=self.start_crawl)

    def start_crawl(self, response):
        yield Request(self.test_url, callback=self.parse_item)

    def parse_item(self, response):
        print("Test URL " + response.url)

But it still doesn't work, giving the error:

ValueError: No element found in <200 https://www.weibo.com/overseas>

Would really appreciate any help anybody can offer as this is kind of beyond my range of knowledge.

0

There are 0 best solutions below