python-爬虫(今天练习的几个小项目)

爬取豆瓣电影分类排行榜

##爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import requests
import urllib.request
if __name__ == "__main__":

    #指定ajax-get请求的url(通过抓包进行获取)
    url = 'https://movie.douban.com/j/chart/top_list?'

    #定制请求头信息,相关的头信息必须封装在字典结构中
    headers = {
        #定制请求头中的User-Agent参数,当然也可以定制请求头中其他的参数
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
    }

    #定制get请求携带的参数(从抓包工具中获取)
    param = {
        'type':'5',
        'interval_id':'100:90',
        'action':'',
        'start':'0',
        'limit':'20'
    }
    #发起get请求,获取响应对象
    response = requests.get(url=url,headers=headers,params=param)

    #获取响应内容:响应内容为json串
    print(response.text)

爬取搜狗知乎词条信息

import requests
import os
#指定搜索关键字
word = input('enter a word you want to search:')
#指定起始页码
start_page = int(input('enter start page num:'))
end_page = int(input('enter end page num:'))
#自定义请求头信息
headers={
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
    }
#指定url
url = 'https://zhihu.sogou.com/zhihu'
#创建文件夹
if not os.path.exists('./sougou'):
    os.mkdir('./sougou')
for page in range(start_page,end_page+1):
    #封装get请求参数
    params = {
        'query':word,
        'ie':'utf-8',
        'page':str(page)
    }
    #发起post请求,获取响应对象
    response = requests.get(url=url,params=params)
    #获取页面数据
    page_text = response.text
    fileName = word+'_'+str(page)+'.html'
    filePath = './sougou/'+fileName
    with open(filePath,'w',encoding='utf-8') as fp:
        fp.write(page_text)
        print('爬取'+str(page)+'页结束')

爬取剑来贴吧前20页

from urllib import request
from urllib.parse import urlencode
import os

#创建文件夹

path='/var/www/html/'
if not os.path.exists(path):
    os.mkdir(path)
    
def down_load_page_html(startPage,endPage,keyword):
    #构建一个headers
    req_header = {
        'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0',
    }
    for i in range(startPage,endPage+1):
        print('正在下载'+str(i)+'页')

        parmas = {
            'kw':keyword,
            'pn':(i-1)*50,
        }
        #使用urlencode,将参数转为url编码格式
        parmas_str = urlencode(parmas)
        full_url = 'https://tieba.baidu.com/f?'+parmas_str
        #构建一个request对象
        req = request.Request(url=full_url,headers=req_header)
        #根据构建一个request对象,发起请求
        response = request.urlopen(req)

        filename = keyword+str(i)+'.html'
        
        filpath=path+filename
             
        html_str = response.read().decode('utf-8')

        with open(filpath,'w') as file:
            print('正在写入' + str(i) + '页')
            file.write(html_str)


if __name__ == '__main__':
    startPage = int(input('请输入起始页'))
    endPage = int(input('请输入截止页'))
    #输入要搜搜的关键字
    keyword = '剑来'
    down_load_page_html(startPage,endPage,keyword)

机智的你已经发现了,放在了apache的默认目录,嘿嘿嘿,网站存放位置

肯德基餐厅信息

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import requests
import urllib.request
if __name__ == "__main__":

    #指定ajax-post请求的url(通过抓包进行获取)
    url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'

    #定制请求头信息,相关的头信息必须封装在字典结构中
    headers = {
        #定制请求头中的User-Agent参数,当然也可以定制请求头中其他的参数
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
    }

    #定制post请求携带的参数(从抓包工具中获取)
    data = {
        'cname':'',
        'pid':'',
        'keyword':'山东',
        'pageIndex': '1',
        'pageSize': '10'
    }
    #发起post请求,获取响应对象
    response = requests.get(url=url,headers=headers,data=data)

    #获取响应内容:响应内容为json串
    print(response.text)

明日目标:
数据解析三种方式
一.正解解析
二.Xpath解析
三.BeautifulSoup解析

下一阶段计划:
scrapy框架的学习
需求:使用scrapy完后对糗事百科的爬取,并存储进数据库。
需求2:使用django框架在前端进行爬取结果的展示。
加油!少年!

    原文作者:wangcc_sd
    原文地址: https://www.jianshu.com/p/06ec0fa19e56
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞