爬当当“python图书”

其实用requests模块爬取数据再熟悉不过了,但是这次分享的是xpath中的一些获取方法,实操一下熟悉用法。

直接上代码👇:

import requests
from lxml import etree
import re
import pymongo

url = "http://search.dangdang.com/?key=python&category_path=01.00.00.00.00.00&page_index=1"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3610.2 Safari/537.36"
}
# client = pymongo.MongoClient("mongodb://localhost:27017")
# client.admin.authenticate("admin", "admin")
# db = client["db_dangdang"]
# coll = db["book"]

response = requests.get(url=url, headers=headers)
response.encoding = "gbk"

html = etree.HTML(response.text)
all_info = html.xpath("//ul[@class='bigimg']//li")

num_re = re.compile(r"(\d+)")
date_re = re.compile(r"\s/(.*)")

for item in all_info:
    info = {}
    info["title"] = item.xpath("./p[@class='name']/a/@title")[0]
    info["price"] = item.xpath("./p[@class='price']")[0].xpath("string(.)")
    info["author"] = item.xpath("./p[@class='search_book_author']/span[1]")[0].xpath("string(.)")

    info["date"] = date_re.findall(item.xpath("./p[@class='search_book_author']/span[2]/text()")[0])[0]
    # info["date"] = item.xpath("./p[@class='search_book_author']/span[2]/text()")[0].strip(" /")

    info["publisher"] = item.xpath("./p[@class='search_book_author']/span[3]/a/text()")[0]

    info["star"] = int(num_re.findall(item.xpath("./p[@class='search_star_line']/span/span/@style")[0])[0])/20
    # info["star"] = int((item.xpath("./p[@class='search_star_line']/span/span/@style")[0].strip("width: %;"))) / 20
    
    try:
        info["detail"] = item.xpath("./p[@class='detail']/text()")[0]
    except:
        info["detail"] = ""
    print(info)
    # coll.insert_one(info)

解析:
1️⃣:如上代码中第28行,获取author数据,正常使用xpath语句获取的话会以分段的方式获取结果,如下图所示:
在这里插入图片描述
而使用.xpath("string(.)")方法,可以将这些分段的字符串内容进行拼接,形成一个完整的整体。

2️⃣:如上代码第30行,获取date数据,结果为:“ /2016-07-01”,因此编写正则表达式,只获取日期部分。(注:编写正则时最好是将需要更改的字符串放在sublime中用ctrl+f来编写正则表达式,以确保无误)
【另外可以不用正则来去除时间前面的 “/” ,可以用strip()方法,里面传入参数 " /"即:strip(" /")。如上代码第31行】

3️⃣:如上代码第35行,获取star数据,结果为:with:95%,同样的需要编写正则表达式来获取其中的数字。
【另外可以不用正则来去除时间前面的 “with:” 和后面的 "%;" ,可以用strip()方法,里面传入参数 "width: %;"即:strip("width: %;")。如上代码第36行】

【注: strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列】
另外,使用 scrapy爬取当当图书,可参考该文章:https://www.cnblogs.com/CYHISTW/p/12377124.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Selenium库可以方便地当当网上的Python书籍信息。Selenium是一个自动化测试工具,可以模拟用户在浏览器上的操作,通过它可以实现自动登录网页、点击页面元素、获取网页内容等功能。 首先,我们需要安装Selenium库并下载相应的驱动程序来控制浏览器。常用的浏览器驱动有ChromeDriver和GeckoDriver,我们选择其中之一安装。 安装完成后,导入Selenium库,并创建一个浏览器实例。 ```python from selenium import webdriver driver = webdriver.Chrome() # 使用Chrome浏览器驱动 # 或者使用GeckoDriver # driver = webdriver.Firefox() ``` 接下来,使用浏览器实例打开当当网的Python书籍页面。 ```python driver.get("http://search.dangdang.com/?key=python&act=input") ``` 然后,我们可以通过Selenium的find_elements方法选取页面上的书籍元素,并提取出我们需要的书籍信息。 ```python book_elements = driver.find_elements_by_class_name("line1") # 通过class_name属性选取书籍元素 for book_element in book_elements: book_title = book_element.find_element_by_tag_name("a").text # 选取书籍标题 book_author = book_element.find_element_by_class_name("search_book_author").text # 选取书籍作者 book_price = book_element.find_element_by_class_name("search_now_price").text # 选取书籍价格 print(book_title, book_author, book_price) ``` 最后,关闭浏览器实例。 ```python driver.quit() ``` 通过以上步骤,我们已经成功使用Selenium取了当当网上的Python书籍信息。需要注意的是,使用Selenium进行虫时,为了避免被网站封禁,可以设置合理的访问频率,并设置合适的User-Agent,模拟真实用户的行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值