爬虫之获取url中的数据参数方法

如果有些参数无法在爬虫获取的数据中找到可以尝试将这些数据写入到url中然后在在后续的解析过程中取出

import urllib.parse

def parse(self, response): city = urllib.parse.unquote(response.url.strip().split("cit=")[-1]) import json cinema_dic = json.loads(response.body.decode('utf8')) for cinema in cinema_dic['cinemas']: yield Request(url='https://baidu.com/ccc/{0}?ci={1}'.format(cc['id'], city), callback=self.parse_index, headers={ 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, ' 'like Gecko) Chrome/70.0.3538.77 Safari/537.36', }) def parse_index(self, response): """ 当起始URL下载完毕后,自动执行parse函数:response封装了响应相关的所有内容。 :param response: :return: """ city = urllib.parse.unquote(response.url.strip().split("ci=")[-1]) #获取上一步中的写入到url中的参数

 

posted on 2019-01-29 17:47  王大拿  阅读(1379)  评论(0)    收藏  举报

导航