python 爬虫:爬取91job竞赛题库

#http://ycit.91job.gov.cn/contest/question

#本次爬去的是91job竞赛的题库

#共52题

#爬去题目与正确答案

#保存为doc格式

#由于需要登陆所以我采用的是cookie

#但是可能在你使用这部分代码是cookie已经失效了 你可以用自己的账号

#xpath是一款十分好用的提取数据的方法

#由于爬取简单就直接上代码了

import requests
from lxml import etree 
import csv
for i in range(1,523):
    url = 'http://ycit.91job.gov.cn/contest/question?page=%s'% i
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko Core/1.70.3676.400 QQBrowser/10.4.3505.400',
    
    'Cookie':'__51cke__=; PHPSESSID2=o0g27b9lvje92ei8f8jd1muht3; __tins__18735067=%7B%22sid%22%3A%201559039651245%2C%20%22vd%22%3A%205%2C%20%22expires%22%3A%201559041694835%7D; __51laig__=5'
    }
    response = requests.get(url = url,headers=header)
    html = response.text
    selector = etree.HTML(html)
    items = selector.xpath('//div[@class="all"]')
    for item in items:
        timu = item.xpath('./div[@class="title"]/b/text()')[0]
        answer = item.xpath('./div[@class="right"]/p/font//text()')[0]
        timu = timu.replace('\xa0'and'\u2002',' ')
        
        info = [timu,answer]
        with open('a.doc','a+',encoding='utf-8',newline='')as f:
            f.write('\n'.join(info)+'\n')

爬取结果::

python 爬虫:爬取91job竞赛题库_第1张图片

 

你可能感兴趣的:(Python爬虫开发,爬虫开发)