温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Python怎么爬取淘宝的商品信息

发布时间:2021-05-06 17:13:02 来源:亿速云 阅读:316 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关使用Python怎么爬取淘宝的商品信息,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

python有哪些常用库

python常用的库:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

1、导入需要的包

import timefrom bs4 
import BeautifulSoupfrom selenium 
import webdriver

2、编辑淘宝数据抓取的函数逻辑

   # 编辑淘宝数据抓取的函数逻辑
    """
    1.登录淘宝
    2.首页
    3.指定商品的搜索信息
    4.提取指定商品的金额、购买人数、商铺地址、商品名称、商品图片
    """

3、打开淘宝网页,并扫码登陆点击淘宝网首页

    def login_info(self):
        # 1.打开网页
        self.browser.get(self.url)

        # 2.通过扫码的形式去登录淘宝账号
        if self.browser.find_element_by_xpath('//*[@id="login"]/div[1]/i'):
            self.browser.find_element_by_xpath('//*[@id="login"]/div[1]/i').click()
        # 让程序等待休眠5秒,通过手机扫码登录
        time.sleep(8)

        # 3.点击淘宝网首页
        taobao_index = self.browser.find_element_by_xpath('//*[@id="J_SiteNavHome"]/div/a')
        taobao_index.click()
        time.sleep(1)

4、自动的在淘宝首页中输入自己想要搜索的商品名称,并且自动点击搜索

        search_input = self.browser.find_element_by_xpath('//*[@id="q"]')
        shop_name = input("请输入你想搜索的商品名称:")
        search_input.send_keys(shop_name)
        time.sleep(0.5)
        search_submit = self.browser.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button')
        search_submit.click()

5、获取商品信息

 # 商品金额
                shop_price_data = shop_data.find_all('div', class_='price g_price g_price-highlight')
                for shop_price in shop_price_data:
                    shop_price_list.append(shop_price.text.strip())

                # 购买人数
                shop_people_number_data = shop_data.find_all('div','deal-cnt')
                for shop_people_number in shop_people_number_data:
                    shop_people_list.append(shop_people_number.text)

                # 店铺地区
                shop_location_data = shop_data.find_all('div','location')
                for shop_location in shop_location_data:
                    shop_location_list.append(shop_location.text)

上述就是小编为大家分享的使用Python怎么爬取淘宝的商品信息了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI