老师,为什么我携带cookie进行登录之后代码运行没有返回数据 啊
import scrapy
class AppSpider(scrapy.Spider):
name = "app"
# allowed_domains = ["www.zhipin.com"]
# start_urls = ['http://search.dangdang.com/?key=%B2%E9%C0%ED%BE%C5%CA%C0&act=input&page_index=1']
def new_req(self):
url = 'http://search.dangdang.com/?key=%B2%E9%C0%ED%BE%C5%CA%C0&act=input&page_index=1'
cookie = '__permanent_id=20230920231611757816761566318461699; ddscreen=2; __visit_id=20231010130051214325074464524625078; __out_refer=; dest_area=country_id%3D9000%26province_id%3D111%26city_id%3D0%26district_id%3D0%26town_id%3D0; pos_6_start=1696916914135; pos_6_end=1696916914801; pos_9_end=1696918094300; ad_ids=12144306%2C3539000%2C3415467%7C%233%2C3%2C3; sessionID=pc_1ced29b229d6290ad28430e58c401e729cf89c5c7d9683da833f94e844e48c0e; USERNUM=KYktRh56D4dUdZlNxID1Kg==; login.dangdang.com=.ASPXAUTH=1kUydnmmYRdqIlrVed3mVv18ivPovzVc3NyaGVO/DOohvFfXaj9XVg==; dangdang.com=email=ZDA3NjU1NWFlOTE4NWRlN0BkZG1vYmlsZV91c2VyLmNvbQ==&nickname=&display_id=2358298867535&customerid=l8UnhPkhztLqXMfVrAcE7A==&viptype=c/5GHQl/FjE=&show_name=133****7052; ddoy=email=d076555ae9185de7@ddmobile_user.com&nickname=&validatedflag=0&uname=13337577052&utype=0&.ALFG=off&.ALTM=1696919792557; LOGIN_TIME=1696919792858; __rpm=%7Cs_112100...1696919801787; __trace_id=20231010143643407244866958314587608'
cookie = {item.split("=")[0]: item.split("=")[1] for item in cookie.split('; ')}
yield scrapy.Request(url=url, callback=self.parse, cookies=cookie)
def warn_on_generator_with_return_value_stub(spider, callable):
pass
scrapy.utils.misc.warn_on_generator_with_return_value = warn_on_generator_with_return_value_stub
scrapy.core.scraper.warn_on_generator_with_return_value = warn_on_generator_with_return_value_stub
def parse(self, response):
print('------------------------------')
link = response.xpath('.//ul[@class="bigimg"]/li')
for i in link:
urls = i.xpath('.//a/@href').get()
urls = 'http:' + urls
yield scrapy.Request(url=urls, callback=self.parse_init)
def parse_init(self, response):
title = response.xpath('/html/body/div[2]/div[3]/div[2]/div/div[1]/div[1]/h1/text()').get()
price = response.xpath("/html/body/div[2]/div[3]/div[2]/div/div[1]/div[6]/div[2]/div[1]/div[1]/p[2]/text()").get()
author = response.xpath('/html/body/div[2]/div[3]/div[2]/div/div[1]/div[2]/span[1]/a/text()').get()
press = response.xpath('/html/body/div[2]/div[3]/div[2]/div/div[1]/div[2]/span[2]/a/text()').get()
store_name = response.xpath('/html/body/div[2]/div[3]/div[2]/div/div[2]/div[2]/p[1]/span/span[2]/span/a/text()').get()
print(title)
def parse_na(self, response):
num = 2
while num <= 100:
url = f'http://search.dangdang.com/?key=%B2%E9%C0%ED%BE%C5%CA%C0&act=input&page_index={num}'
num = num + 1
yield scrapy.Request(url=url, callback=self.parse)
#
# with open('./cl.txt', 'w', encoding='utf-8') as f:
# f.write(htmls)
8
收起
正在回答 回答被采纳积分+1
1回答

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星