scrapy---爬取某招聘网站遇到的问题小结

爬取时一直被重定向问题困扰 REDIRECT[302] 像知乎之类的网站一般爬取是需要User-Agent的 但是 对于拉勾网来说,并不必须UA。

后来搜索了一下 需要提供cookie

解决重定向问题:

customer-settings =

{

"COOKIES_ENABLED": False,

"DOWNLOAD_DELAY": 1,

'DEFAULT_REQUEST_HEADERS': {

'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',

'Accept-Encoding': 'gzip, deflate, br',

'Accept-Language': 'zh-CN,zh;q=0.8',

'Connection': 'keep-alive',

'Cookie':'user_trace_token=20180809145407-031d6690-9ba1-11e8-b9cd-525400f775ce; LGUID=20180809145407-031d6aa7-9ba1-11e8-b9cd-525400f775ce; LG_LOGIN_USER_ID=1d2009da1adcd1e1b3df90ae486e0a6ed6f927d9d0ad0806; JSESSIONID=ABAAABAAADEAAFIAE5968C60EFF2D413ECDB2FF8F13ABCB; WEBTJ-ID=20180813160154-165324e448fcc9-071b34d843c314-102c1709-1049088-165324e4490cd2; PRE_UTM=m_cf_cpt_baidu_pc; PRE_HOST=www.baidu.com; PRE_SITE=https%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3D%25E6%258B%2589%25E5%258B%25BE%25E7%25BD%2591%26rsv_spt%3D1%26rsv_iqid%3D0xb29d323700012351%26issp%3D1%26f%3D3%26rsv_bp%3D0%26rsv_idx%3D2%26ie%3Dutf-8%26tn%3Dbaiduhome_pg%26rsv_enter%3D1%26rsv_sug3%3D1%26rsv_sug1%3D1%26rsv_sug7%3D100%26rsv_sug2%3D0%26prefixsug%3D%2525E6%25258B%252589%2525E5%25258B%2525BE%26rsp%3D0%26inputT%3D1498%26rsv_sug4%3D1498; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Flp%2Fhtml%2Fcommon.html%3Futm_source%3Dm_cf_cpt_baidu_pc; _putrc=3D7BB7F7DC9382C5; login=true; unick=maxinehehe; showExpriedIndex=1; showExpriedCompanyHome=1; showExpriedMyPublish=1; hasDeliver=0; gate_login_token=221b6ccc9989e5db7131c21fc68d1b90499483888aeea7d6; TG-TRACK-CODE=index_navigation; SEARCH_ID=f386beab95164a8e8e860aa82b4bd751; index_location_city=%E5%8C%97%E4%BA%AC; _gid=GA1.2.1333500619.1534062851; _ga=GA1.2.504828216.1533797648; LGSID=20180813160155-24f4dafe-9ecf-11e8-bb57-525400f775ce; LGRID=20180813160218-331abb23-9ecf-11e8-a37b-5254005c3644; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1533797648,1534062851,1534147148,1534147316; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1534147348',

'Host': 'www.lagou.com',

'Origin': 'https://www.lagou.com',

'Referer': 'https://www.lagou.com/',

'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36',

}

}

以上内容 打开浏览器 打开要爬取的网站 >>> 检查 >>> Network 没有内容的刷新一下 拉上去 一般点击第一个即可 会出现如下内容:

说明 我使用的是chrome浏览器 其他浏览器 大同小异

 

Twisted问题:Unhandled error in Deferred:

试着在网上找了下,结果基本全是让安装pywin32 我是linux........

还有就是让更新twisted也是没解决问题,在stackflow上有类似的 也没多大帮助

还是老老实实读错误信息吧,

Unhandled error in Deferred:

2018-08-13 17:17:36 [twisted] CRITICAL: Unhandled error in Deferred:

 

2018-08-13 17:17:36 [twisted] CRITICAL:

Traceback (most recent call last):

File "/home/maxinehehe/.virtualenvs/article_spider/lib/python3.5/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks

current.result = callback(current.result, *args, **kw)

TypeError: handle_error() missing 2 required positional arguments: 'item' and 'spider'

handle_error()少了两个元素,网上找 追到错误信息 发现在 addErrback(self.handle_error)

这是之前的查找错误信息,后面写的时候忘记更改了

def process_item(self, item, spider):

    # 使用twisted将mysql插入编程异步操作

    # 指定操作方法和操作的数据 [下面会将方法异步处理]

    query = self.dppool.runInteraction(self.do_insert, item)

    # AttributeError: 'Deferred' object has no attribute 'addErrorback'

    # query.addErrorback(self.handle_error) # 处理异常

 

    # query.addErrback(self.handle_error) # 处理异常 爬取拉勾出错的地方

 

    query.addErrback(self.handle_error, item, spider)

添加item, spider 错误解决。

 

后面还碰到一个

[Failure instance: Traceback: <class '_mysql_exceptions.ProgrammingError'>: not all arguments converted during string formatting

String格式 问题 出错

原因是 少了个%s匹配 在写insert_sql语句时 采用 format形式 出现 这种错误 我也是醉了 。

VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE

job_desc=VALUES(job_desc)

 

解释:ON DUPLICATE KEY UPDATE job_desc=VALUES(job_desc)

意思是 如果数据库已经存在当前插入数据 就更新 job_desc

也可以更新其他值 写法同上 以逗号“ ,”隔开

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值