如何获得Scrapy请求以转到下一页?

时间:2019-01-19 00:20:22

标签: python-3.x scrapy

我需要在web page中解析每个评论的国家/地区代码,然后将其存储在json文件中,但是当我尝试转到下一页时遇到问题。

我不确定我是否使用正确的方式发送请求。

这是我的代码:

index = 1
def parse_fb(self, response):

    data = response.body
    soup = BeautifulSoup(data, "html.parser")

    with open(ArticlesSpider.pro_id+'.json', 'a+') as f:
        user_country = soup.find_all('div', class_='user-country')
        for i in range(len(user_country)):
            code = str(user_country[i])
            code = code.split('">')
            code = str(code[2])
            code = code.split('</b>')
            code = code[0]
            json.dump(code, f)
            print(code)
    request_url='https://feedback.aliexpress.com/display/productEvaluation.htm'
    data = {
            'ownerMemberId': '',
            'memberType':'seller',
            'productId': str(ArticlesSpider.pro_id),
            'companyId': '',
            'evaStarFilterValue': 'all Stars',
            'evaSortValue': 'sortdefault@feedback',
            'page': str(index),
            'currentPage': '',
            'startValidDate': '',
            'i18n': 'false',
            'withPictures': 'false',
            'withPersonalInfo': 'false',
            'withAdditionalFeedback': 'false',
            'onlyFromMyCountry': 'false',
            'version': 'evaNlpV1_2',
            'isOpened': 'true',
            'translate':  'Y',
            'jumpToTop':'false',
            '${csrfToken.parameterName}': '${csrfToken.token}',
            }
    index += 1
    yield scrapy.FormRequest(request_url,formdata=data,callback=self.parse_fb)

2 个答案:

答案 0 :(得分:0)

好吧,您正在更改<android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/fab" android:tint="@android:color/white" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" app:fabSize="mini" android:layout_alignParentRight="true" android:src="@drawable/ic_send_black_24dp"/> ,但没有使用它:index在此过程中是相同的。如果此位是您希望更改页面的位

request_url

比您在致电之前必须更改yield scrapy.FormRequest(request_url,formdata=data,callback=self.parse_fb)

答案 1 :(得分:0)

您为什么需要BeautifulSoup?所有这些都是多余的。 这是您产品的工作代码:

import scrapy


class CodeInfo(scrapy.Item):
    code = scrapy.Field()


class feedback_aliexpress_com(scrapy.Spider):
    name = 'feedback_aliexpress_com'
    domain = 'feedback.aliexpress.com'
    allowed_domains = ['feedback.aliexpress.com']
    start_urls = ['https://feedback.aliexpress.com/display/productEvaluation.htm?' +
                  'productId=32911361727&ownerMemberId=206054366&companyId=&memberType=seller&startValidDate=']
    url = 'https://feedback.aliexpress.com/display/productEvaluation.htm'

    page = 1

    def parse(self, response):
        code = CodeInfo()
        if response.css('.user-country'):
            for listing in response.css('.feedback-item'):
                code['code'] = listing.css('.user-country > b::text').extract_first()

                yield code
            self.page += 1
            self.url = 'https://feedback.aliexpress.com/display/productEvaluation.htm?productId=32911361727&ownerMemberId=206054366&page=' \
                       + str(self.page)
            yield response.follow(url=self.url, callback=self.parse)

很多多余的东西)))我知道)))检查一下)赶时间