Python爬虫实战之爬取某宝男装信息
知识点介绍
本次爬取用到的知识点有:
1. selenium
2. pymysql
3 pyquery
实现步骤
1. 分析目标网站
1. 打开某宝首页, 输入"男装"后点击"搜索", 则跳转到"男装"的搜索界面.
2. 空白处"右击"再点击"检查"审查网页元素, 点击"network".
1) 找到对应的url, url里的参数正是query string parameters的参数, 且请求方式是get
2) 我们请求该url得到内容就是"response"里的内容, 那么点击它来确认信息.
3) 下拉看到"男装"字样, 那么再往下找, 并没有发现有关"男装"的商品信息.
4) 任意复制一个商品信息, 空白处右击再点击"查看网页源代码", 在源码查找该商品, 即可看到该商品的信息.
5) 对比网页源代码和"response"响应内容, 发现源代码<script>..........</script>中的商品信息被替换, 这便是采用了js加密
6) 如果去请求上面的url, 得到的则是加密过的信息, 这时就可以利用selenium库来模拟浏览器, 进而得到商品信息.
2. 获取单个商品界面
请求网站
# -*- coding: utf-8 -*- from selenium import webdriver #从selenium导入浏览器驱动 browser = webdriver.chrome() #声明驱动对象, 即chrome浏览器 def get_one_page(): '''获取单个页面''' browser.get("https://www.xxxxx.com") #请求网站
输入"男装", 在输入之前, 需要判断输入框是否存在, 如果存在则输入"男装", 不存在则等待显示成功.
# -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.common.by import by #导入元素定位方法模块 from selenium.webdriver.support.ui import webdriverwait #导入等待判断模块 from selenium.webdriver.support import expected_conditions as ec #导入判断条件模块 browser = webdriver.chrome() def get_one_page(): '''获取单个页面''' browser.get("https://www.xxxxx.com") input = webdriverwait(browser,10).until( #等待判断 ec.presence_of_element_located((by.css_selector,"#q"))) #若输入框显示成功,则获取,否则等待 input.send_keys("男装") #输入商品名称
下一步就是点击"搜索"按钮, 按钮具有属性: 可点击, 那么加入判断条件.
# -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.common.by import by from selenium.webdriver.support.ui import webdriverwait from selenium.webdriver.support import expected_conditions as ec browser = webdriver.chrome() def get_one_page(): '''获取单个页面''' browser.get("https://www.xxxxx.com") input = webdriverwait(browser,10).until( ec.presence_of_element_located((by.css_selector,"#q"))) # input.send_keys("男装") button = webdriverwait(browser,10).until( #等待判断 ec.element_to_be_clickable((by.css_selector,"#j_tsearchform > div.search-button > button"))) #若按钮可点击, 则获取, 否则等待 button.click() #点击按钮
获取总的页数, 同样加入等待判断.
# -*- coding: utf-8 -*- import re from selenium import webdriver from selenium.common.exceptions import timeoutexception from selenium.webdriver.common.by import by from selenium.webdriver.support.ui import webdriverwait from selenium.webdriver.support import expected_conditions as ec browser = webdriver.chrome() def get_one_page(): '''获取单个页面''' browser.get("https://www.xxxxx.com") input = webdriverwait(browser, 10).until( ec.presence_of_element_located((by.css_selector, "#q"))) input.send_keys("男装") button = webdriverwait(browser, 10).until( ec.element_to_be_clickable( (by.css_selector, "#j_tsearchform > div.search-button > button"))) button.click() pages = webdriverwait(browser, 10).until( # 等待判断 ec.presence_of_element_located( (by.css_selector, "#mainsrp-pager > div > div > div > div.total"))) # 若总页数加载成功,则获取总页数,否则等待 return pages.text def main(): pages = get_one_page() print(pages) if __name__ == '__main__': main()
打印出来的不是我们想要的结果, 利用正则表达式获取, 最后再利用try...except捕捉异常
# -*- coding: utf-8 -*- import re from selenium import webdriver from selenium.common.exceptions import timeoutexception from selenium.webdriver.common.by import by from selenium.webdriver.support.ui import webdriverwait from selenium.webdriver.support import expected_conditions as ec browser = webdriver.chrome() def get_one_page(): '''获取单个页面''' try: browser.get("https://www.xxxxx.com") input = webdriverwait(browser,10).until( ec.presence_of_element_located((by.css_selector,"#q"))) input.send_keys("男装") button = webdriverwait(browser,10).until( ec.element_to_be_clickable((by.css_selector,"#j_tsearchform > div.search-button > button"))) button.click() pages = webdriverwait(browser,10).until( ec.presence_of_element_located((by.css_selector,"#mainsrp-pager > div > div > div > div.total"))) return pages.text except timeoutexception: return get_one_page() #如果超时,继续获取 def main(): pages = get_one_page() pages = int(re.compile("(\d+)").findall(pages)[0]) #采用正则表达式提取文本中的总页数 print(pages) if __name__ == '__main__': main()
关于selenium的更多内容,可参看官方文档https://selenium-python.readthedocs.io/waits.html
3. 获取多个商品界面
采用获取"到第 页"输入框方式, 切换到下一页, 同样是等待判断
需要注意的是, 最后要加入判断: 高亮是否是当前页
def get_next_page(page): try: input = webdriverwait(browser, 10).until( ec.presence_of_element_located((by.css_selector, "#mainsrp-pager > div > div > div > div.form > input"))) # 若输入框加载成功,则获取,否则等待 input.send_keys(page) # 输入页码 button = webdriverwait(browser, 10).until( ec.element_to_be_clickable((by.css_selector, "#mainsrp-pager > div > div > div > div.form > span.btn.j_submit"))) # 若按钮可点击,则获取,否则等待 button.click() # 点击按钮 webdriverwait(browser,10).until( ec.text_to_be_present_in_element((by.css_selector,"#mainsrp-pager > div > div > div > ul > li.item.active > span"),str(page))) # 判断高亮是否是当前页 except timeoutexception: # 超时, 继续请求 return get_next_page(page) def main(): pages = get_one_page() pages = int(re.compile("(\d+)").findall(pages)[0]) for page in range(1,pages+1): get_next_page(page) if __name__ == '__main__': main()
4. 获取商品信息
首先, 判断信息是否加载成功, 紧接着获取源码并初始化, 进而解析.
需要注意的是, 在"get_one_page"和"get_next_page"中调用之后, 才可执行
def get_info(): """获取详情""" webdriverwait(browser,20).until(ec.presence_of_element_located(( by.css_selector,"#mainsrp-itemlist .items .item"))) #判断商品信息是否加载成功 text = browser.page_source #获取网页源码 html = pq(text) #初始化网页源码 items = html('#mainsrp-itemlist .items .item').items() #采用items方法会得到生成器 for item in items: #遍历每个节点对象 data = [] image = item.find(".pic .img").attr("data-src") #用find方法查找子孙节点,用attr方法获取属性名称 price = item.find(".price").text().strip().replace("\n","") #用text方法获取文本,strip()去掉前后字符串,默认是空格 deal = item.find(".deal-cnt").text()[:-2] title = item.find(".title").text().strip() shop = item.find(".shop").text().strip() location = item.find(".location").text() data.append([shop, location, title, price, deal, image]) print(data)
5. 保存到mysql数据库
def save_to_mysql(data): """存储到数据库""" # 创建数据库连接对象 db= pymysql.connect(host = "localhost",user = "root",password = "password",port = 3306, db = "spiders",charset = "utf8") # 获取游标 cursor = db.cursor() #创建数据库 cursor.execute("create table if not exists {0}(shop varchar(20),location varchar(10),title varchar(255),price varchar(20),deal varchar(20), image varchar(255))".format("男装")) #sql语句 sql = "insert into {0} values(%s,%s,%s,%s,%s,%s)".format("男装") try: #传入参数sql,data if cursor.execute(sql,data): #插入数据库 db.commit() print("********已入库**********") except: print("#########入库失败#########") #回滚,相当什么都没做 db.rollback() #关闭数据库 db.close()
完整代码
# -*- coding: utf-8 -*- ''' 有需要python学习资料的小伙伴吗?小编整理一套python资料和pdf,感兴趣者可以加学习群:548377875,反正闲着也是闲着呢,不如学点东西啦~~ ''' import re import pymysql from selenium import webdriver from selenium.common.exceptions import timeoutexception from selenium.webdriver.common.by import by from selenium.webdriver.support.ui import webdriverwait from selenium.webdriver.support import expected_conditions as ec from pyquery import pyquery as pq browser = webdriver.chrome() def get_one_page(name): '''获取单个页面''' print("-----------------------------------------------获取第一页-------------------------------------------------------") try: browser.get("https://www.xxxxx.com") input = webdriverwait(browser,10).until( ec.presence_of_element_located((by.css_selector,"#q"))) input.send_keys(name) button = webdriverwait(browser,10).until( ec.element_to_be_clickable((by.css_selector,"#j_tsearchform > div.search-button > button"))) button.click() pages = webdriverwait(browser,10).until( ec.presence_of_element_located((by.css_selector,"#mainsrp-pager > div > div > div > div.total"))) print("----即将解析第一页信息----") get_info(name) print("----第一页信息解析完成----") return pages.text except timeoutexception: return get_one_page(name) def get_next_page(page,name): """获取下一页""" print("---------------------------------------------------正在获取第{0}页----------------------------------------".format(page)) try: input = webdriverwait(browser, 10).until( ec.presence_of_element_located((by.css_selector, "#mainsrp-pager > div > div > div > div.form > input"))) input.send_keys(page) button = webdriverwait(browser, 10).until( ec.element_to_be_clickable((by.css_selector, "#mainsrp-pager > div > div > div > div.form > span.btn.j_submit"))) button.click() webdriverwait(browser,10).until( ec.text_to_be_present_in_element((by.css_selector,"#mainsrp-pager > div > div > div > ul > li.item.active > span"),str(page))) print("-----即将解析第{0}页信息-----".format(page)) get_info(name) print("-----第{0}页信息解析完成-----".format(page)) except timeoutexception: return get_next_page(page,name) def get_info(name): """获取详情""" webdriverwait(browser,20).until(ec.presence_of_element_located(( by.css_selector,"#mainsrp-itemlist .items .item"))) text = browser.page_source html = pq(text) items = html('#mainsrp-itemlist .items .item').items() for item in items: data = [] image = item.find(".pic .img").attr("data-src") price = item.find(".price").text().strip().replace("\n","") deal = item.find(".deal-cnt").text()[:-2] title = item.find(".title").text().strip() shop = item.find(".shop").text().strip() location = item.find(".location").text() data.append([shop, location, title, price, deal, image]) for dt in data: save_to_mysql(dt,name) def save_to_mysql(data,name): """存储到数据库""" db= pymysql.connect(host = "localhost",user = "root",password = "password",port = 3306, db = "spiders",charset = "utf8") cursor = db.cursor() cursor.execute("create table if not exists {0}(shop varchar(20),location varchar(10),title varchar(255),price varchar(20),deal varchar(20), image varchar(255))".format(name)) sql = "insert into {0} values(%s,%s,%s,%s,%s,%s)".format(name) try: if cursor.execute(sql,data): db.commit() print("********已入库**********") except: print("#########入库失败#########") db.rollback() db.close() def main(name): pages = get_one_page(name) pages = int(re.compile("(\d+)").findall(pages)[0]) for page in range(1,pages+1): get_next_page(page,name) if __name__ == '__main__': name = "男装" main(name)
到此这篇关于python爬虫实战之爬取某宝男装信息的文章就介绍到这了,更多相关python 爬取信息内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: js 颜色16进制转RGB方法
下一篇: c半同步半异步进程池模型之cgi服务器
推荐阅读
-
Python爬虫实战之爬取某宝男装信息
-
Python爬虫实战用 BeautifulSoup 爬取电影网站信息
-
Python爬虫实战用 BeautifulSoup 爬取电影网站信息
-
python网络爬虫之解析网页的XPath(爬取Path职位信息)[三]
-
Python3爬虫学习之将爬取的信息保存到本地的方法详解
-
Python3爬虫学习之MySQL数据库存储爬取的信息详解
-
Python爬虫项目--爬取某宝男装信息
-
Python爬虫实战之Requests+正则表达式爬取猫眼电影Top100
-
Python爬虫实战案例之爬取喜马拉雅音频数据详解
-
python爬虫--爬取某网站电影信息并写入mysql数据库