Python爬虫-小测验

简介: 一、 使用scrapy.Selector或BeautifulSoup,实现以下需求(30分) (1)读取给定的dangdang.html页面内容,注:编码为gbk(5分) (2)获取页面中所有图书的名称,价格,作者,出版社及图书图片的url地址(...
+关注继续查看

一、 使用scrapy.Selector或BeautifulSoup,实现以下需求(30分)

(1)读取给定的dangdang.html页面内容,注:编码为gbk(5分)
(2)获取页面中所有图书的名称,价格,作者,出版社及图书图片的url地址(20分)
(3)将获取的信息保存至文件(excel、csv、json、txt格式均可)(5分)
网页文件dangdang.html文件下载链接: https://pan.baidu.com/s/1awbG5zqOMdnWzXee7TZm6A 密码: 3urs

1.1使用BeautifulSoup解决

from bs4 import BeautifulSoup as bs
import pandas as pd

def cssFind(book,cssSelector,nth=1):
    if len(book.select(cssSelector)) >= nth:
        return book.select(cssSelector)[nth-1].text.strip()
    else:
        return ''

if __name__ == "__main__":
    with open("dangdang.html",encoding='gbk') as file:
        html = file.read()
    soup = bs(html,'lxml')
    book_list = soup.select("div ul.bigimg li")
    result_list = []
    for book in book_list:
        item = {}
        item['name'] = book.select("a.pic")[0]['title']
        item['now_price'] = cssFind(book,"span.search_now_price")
        item['pre_price'] = cssFind(book,"span.search_pre_price")
        item['author'] = book.select("p.search_book_author a")[0]['title']
        item['publisher'] = book.select("p.search_book_author span a")[-1].text
        item['detailUrl'] = book.select("p.name a")[0]['href']
        item['imageUrl'] = book.select("a.pic img")[0]['src']
        if item['imageUrl'] == "images/model/guan/url_none.png":
            item['imageUrl'] = book.select("a.pic img")[0]['data-original']
        result_list.append(item)

    df = pd.DataFrame(result_list,columns=result_list[0].keys())
    df.to_excel("当当图书信息.xlsx")

1.2使用scrapy.selector解决

from scrapy.selector import Selector
import pandas as pd

if __name__ == "__main__":
    with open("dangdang.html",encoding='gbk') as file:
        response = Selector(text=file.read())
    book_list = response.xpath("//ul[@class='bigimg']/li")
    result_list = []
    for book in book_list:
        item = {}
        item['name'] = book.xpath("a[@class='pic']/@title").extract_first()
        item['now_price'] = book.xpath(".//span[@class='search_now_price']/text()").extract_first()
        item['pre_price'] = book.xpath(".//span[@class='search_pre_price']/text()").extract_first()
        item['author'] = book.xpath("p[@class='search_book_author']//a/@title").extract_first()
        item['publisher'] = book.xpath("p[@class='search_book_author']//a/@title").extract()[-1]
        item['detailUrl'] = book.xpath(".//p[@class='name']/a/@href").extract_first()
        item['imageUrl'] = book.xpath("a[@class='pic']/img/@src").extract_first()
        if item['imageUrl'] == "images/model/guan/url_none.png":
            item['imageUrl'] = book.xpath("a[@class='pic']/img/@data-original").extract_first()
        result_list.append(item)

    df = pd.DataFrame(result_list,columns=result_list[0].keys())
    df.to_excel("当当图书信息.xlsx")

二、 需求:抓取天猫三只松鼠旗舰店超级满减商品信息(55分)

网站地址如下https://sanzhisongshu.tmall.com/p/rd523844.htm?spm=a1z10.1-b-s.w5001-14855767631.8.19ad32fdW6UhfO&scene=taobao_shop
评分标准如下:
1、创建函数获取页面所有内容,代码无误(5分)
2、得到页面内容后解析信息,获取页面中图片链接,并将图片下载至本地photo文件夹。(10分)
3、获取页面中每个商品信息的商品名称、价格以及商品图片url信息(20分)
4、创建数据库product,及表格productinfo,包含(商品名称、价格及图片地址三个字段)(5分)
5、将第(3)步获取的结果写入数据库(10分)
6、代码规范,有注释(5分)

import requests
from bs4 import BeautifulSoup as bs
import urllib
import os
import pymysql

#获取实例化BeautifulSoup对象
def getSoup(url, encoding="gbk", **params):
    reponse = requests.get(url, **params)
    reponse.encoding = encoding
    soup = bs(reponse.text, 'lxml')
    return soup

#下载单个图片函数
def downloadImage(imgUrl, imgName):
    imgDir = "photo"
    if not os.path.isdir(imgDir):
        os.mkdir(imgDir)
    imgPath = "%s/%s" %(imgDir,imgName)
    urllib.request.urlretrieve(imgUrl,imgPath)

#下载所有图片函数
def downloadAllImages(soup):
    image_list = soup.select("img")
    count = 0
    for image in image_list:
        try:
            srcStr = image['data-ks-lazyload']
            imgFormat = srcStr[-3:]
            if imgFormat == 'gif':
                continue
            count += 1
            imgName = "%d.%s" % (count, imgFormat)
            imgUrl = "http:" + srcStr
            downloadImage(imgUrl, imgName)
        except Exception as e:
            print(str(e))

#通过css选择器语法选择出标签
def cssFind(movie,cssSelector,nth=1):
    if len(movie.select(cssSelector)) >= nth:
        return movie.select(cssSelector)[nth-1].text.strip()
    else:
        return ''

#获取数据库连接函数
def getConn(database ="product"):
    args = dict(
        host = 'localhost',
        user = 'root',
        passwd = '.... your password',
        charset = 'utf8',
        db = database
    )
    return pymysql.connect(**args)

if __name__ == "__main__":
    soup = getSoup("https://sanzhisongshu.tmall.com/p/rd523844.htm" \
                   "?spm=a1z10.1-b-s.w5001-14855767631.8.19ad32fdW6UhfO&scene=taobao_shop")
    #下载所有图片
    downloadAllImages(soup)
    #获取数据库连接
    conn = getConn()
    cursor = conn.cursor()
    #新建数据库中的表productinfo
    sql_list = []
    sql_list.append("drop table if exists productinfo")
    sql_list.append("create table productinfo(name varchar(200)," \
                    "price varchar(20),imageUrl varchar(500))")
    for sql in sql_list:
        cursor.execute(sql)
        conn.commit()
    #获取商品信息并插入数据库
    item_list = soup.select("div.item4line1 dl.item")
    for item in item_list:
        name = cssFind(item,"dd.detail a")
        price = cssFind(item,"dd.detail span.c-price")
        imageUrl = item.select("dt img")[0]['data-ks-lazyload']
        insert_sql = 'insert into productinfo values("%s","%s","%s")' %(name,price,imageUrl)
        cursor.execute(insert_sql)
        conn.commit()

三、请以你的理解尽可能准确的描述出scrapy运行的原理图(15分)

img_1e488e96c3ae6ae27c304834f0a1d867.png
scrapy框架原理图.png

在实际编写代码的过程一种,一般按照下列顺序编写代码文件:
1.编写item.py文件;2.编写爬虫文件;3.编写pipelines.py文件;4.编写settings.py文件
在Scrapy框架理解上:
1.爬虫Spiders发送请求Requests给调度器Scheduler
2.调度器Scheduler发送下载网页的请求Requests给下载器Downloader
3.下载器Downloader获取网页相应response交给爬虫Spiders
4.爬虫Spiders对response进行解析形成Item
5.Item传送给管道,管道对数据进行相应处理,数据持久化。
6.Middelwares分为三种:调度中间件Scheduler middlewares、爬虫中间件spider Middlewares、下载中间件Download Middlewares。在编写scrapy-redis分布式爬虫时,redis就相当于调度中间件Scheduler middlewares;对爬虫进行伪装,设置用户代理User-agent和代理Ip,是在爬虫中间件spider Middlewares中进行设置,下载中间件Download Middlewares可以对下载进行相应设置。

目录
相关文章
|
8天前
|
数据采集 存储 大数据
Python爬虫:数据获取与解析的艺术
随着大数据时代的到来,数据的重要性日益凸显。Python作为一种易学易用的编程语言,在数据处理和分析方面有着丰富的库支持。其中,爬虫是获取数据的重要手段之一。本文将介绍Python爬虫的基本概念、常用库以及实战案例。
38 0
|
17天前
|
数据采集 数据安全/隐私保护 Python
python-爬虫-selenium总结
python-爬虫-selenium总结
python-爬虫-selenium总结
|
19天前
|
数据采集 中间件 Shell
Python爬虫深度优化:Scrapy库的高级使用和调优
在我们前面的文章中,我们探索了如何使用Scrapy库创建一个基础的爬虫,了解了如何使用选择器和Item提取数据,以及如何使用Pipelines处理数据。在本篇高级教程中,我们将深入探讨如何优化和调整Scrapy爬虫的性能,以及如何
|
25天前
|
数据采集 Python
python 爬虫 佛山区域,爬取餐厅的商户联系人公开号码,实例脚本
python 爬虫 佛山区域,爬取餐厅的商户联系人公开号码,实例脚本
|
25天前
|
数据采集 JSON 前端开发
Python爬虫进阶:使用Scrapy库进行数据提取和处理
在我们的初级教程中,我们介绍了如何使用Scrapy创建和运行一个简单的爬虫。在这篇文章中,我们将深入了解Scrapy的强大功能,学习如何使用Scrapy提取和处理数据。
|
2月前
|
数据采集 JavaScript API
Python爬虫抓取经过JS加密的API数据的实现步骤
Python爬虫抓取经过JS加密的API数据的实现步骤
|
2月前
|
数据采集 人工智能 Java
Python爬虫获取电子书资源实战
最近在学习Python,相对java来说python简单易学、语法简单,工具丰富,开箱即用,适用面广做全栈开发那是极好的,对于小型应用的开发,虽然运行效率慢点,但开发效率极高。大大提高了咱们的生产力。为什么python能够在这几年火起来,自然有他的道理,当然也受益于这几年大数据和AI的火。据说网络上80%的爬虫都是用python写的,不得不说python写爬虫真的是so easy。基本上一个不太复杂的网站可以通过python用100多行代码就能实现你所需要的爬取。
102 1
Python爬虫获取电子书资源实战
|
2月前
|
数据采集 关系型数据库 MySQL
|
2月前
|
数据采集 存储 中间件
|
2月前
|
数据采集 JavaScript 前端开发
推荐文章
更多