我有一个搜索与给定标题相关的列表的剪贴代码,然后从每个列表的第一页下载其他标题评级。它工作得很好
然而
有没有可能,我可以让这个代码登录我的imdb登录,然后从这些列表下载我自己的评分
代码如下:
class lisTopSpider(scrapy.Spider):
name= 'ImdbListsSpider'
allowed_domains = ['imdb.com']
titleLinkNum = 'tt0120852'
start_urls = [
'https://www.imdb.com/lists/'+titleLinkNum
]
# def ???(self, response):
# return scrapy.FormRequest.from_response(
# formdata={"username": "example@gmail.com","password":"example",}
# callback=self.parse)
#lists related to given title
def parse(self, response):
listsLinks = response.xpath('//div[2]/strong')
for link in listsLinks:
list_url = response.urljoin(link.xpath('.//a/@href').get())
yield scrapy.Request(list_url, callback=self.parse_list, meta={'list_url': list_url})
next_page_url = response.xpath('//a[@class="flat-button next-page "]/@href').get()
if next_page_url is not None:
next_page_url = response.urljoin(next_page_url)
yield scrapy.Request(next_page_url, callback=self.parse)
#Link of each list
def parse_list(self, response):
list_url = response.meta['list_url']
myRatings = response.xpath('//div[@class="ipl-rating-star small"]/span[2]/text()').getall()
yield{
'list': list_url,
'ratings': myRatings,
}
很可能您想要的是^{} ,它允许您,而不是Scrapy,组成启动爬网的初始
Request
对象。他们的示例或多或少与您的伪代码匹配或者,您也可以不使用
FormRequest
提交凭据,而是通过浏览器单独登录,获取身份验证cookie,并通过start_requests
提供这些cookie,以防登录时出现任何奇怪的情况(如reCaptcha、双因素身份验证等):并确保您的蜘蛛的}
settings.py
中有^{相关问题 更多 >
编程相关推荐