用selenium模拟浏览器爬取淘宝商品信息

有两种搜索商品的方法,一种是在首页进行输入和点击操作,第二种是直接用url进行搜索,显然直接用url进行搜索会更好。避免了麻烦

from selenium import webdriver
from lxml import etree
import pymongo
import sys

global key_word
global browser
print('请输入你想要查询的商品:')

def get_entrance(url):
    browser.get(url)
    browser.implicitly_wait(10)
    browser.find_element_by_id('q').send_keys(key_word)
    browser.find_element_by_class_name('btn-search tb-bg').click()


if __name__ == '__main__':
    key_word = sys.stdin.readline()
    browser = webdriver.Chrome()
    browser.maximize_window()
    url = 'https://www.taobao.com'
    get_entrance(url)
    #测试成功

反正爬虫就是解析网页源代码和翻页而已嘛,selenium就是模拟人的操作处理这两者
先获取第一页把信息解析好再进行翻页处理,翻页处理的话,页面上有三个翻页按钮,哪一个都是可以的,只要用js方法。。。直接用selenium的click()方法真的烦扰了我很久都不行。上网查了一下js方法就可解决:构造一条js语句,然后用browser.execute_script(js)即可。

from selenium import webdriver
from time import sleep
from urllib.parse import quote

browser = webdriver.Chrome()
browser.maximize_window()

url = 'https://s.taobao.com/search?q=' + quote('生物')
browser.implicitly_wait(6)

# 结合xpath helper和 copy xpath一起
while True:
    browser.get(url)
    sleep(10)
    browser.implicitly_wait(10)
    goods = browser.find_elements_by_xpath('//div[contains(@class, "J_MouserOnverReq")]')
    for good in goods:
        price = good.find_element_by_xpath('div[2]/div[1]/div[1]/strong').text
        number_of_sell = good.find_element_by_xpath('div[2]/div[1]/div[2]').text
        name = good.find_element_by_xpath('div[2]/div[2]/a').text.strip()
        shop_name = good.find_element_by_xpath('div[2]/div[3]/div[1]/a/span[2]').text
        location = good.find_element_by_xpath('div[2]/div[3]/div[2]').text
        data = {
            'price':price + '¥',
            'number_of_sell':number_of_sell,
            'name':name,
            'shop_name':shop_name,
            'location':location,
        }
        print(data)#测试成功,第一页爬取成功,下面进行翻页处理
    break

browser.quit()

测试成功,加上翻页处理就好了。。==由于尝试太多,还是会被发现是selenium的,当尝试了多次。。老马就来找你麻烦了,要你滑滑块了。。

from selenium import webdriver
from time import sleep
from urllib.parse import quote
import sys

browser = webdriver.Chrome()
browser.maximize_window()

def judge_exit(page,depth):
    return True if page > depth else False

def get_info(page,depth):
    page += 1
    if judge_exit(page,depth):
        sys.exit('Bye~')
    browser.get(browser.current_url)
    sleep(2)
    browser.implicitly_wait(5)
    goods = browser.find_elements_by_xpath('//div[contains(@class, "J_MouserOnverReq")]')
    for good in goods:
        price = good.find_element_by_xpath('div[2]/div[1]/div[1]/strong').text
        number_of_sell = good.find_element_by_xpath('div[2]/div[1]/div[2]').text
        name = good.find_element_by_xpath('div[2]/div[2]/a').text.strip()
        shop_name = good.find_element_by_xpath('div[2]/div[3]/div[1]/a/span[2]').text
        location = good.find_element_by_xpath('div[2]/div[3]/div[2]').text
        data = {
            'price': price + '¥',
            'number_of_sell': number_of_sell,
            'name': name,
            'shop_name': shop_name,
            'location': location,
        }
        print(data)
    go_to_next_page(page,depth)

def go_to_next_page(page,depth):
    # botton = browser.find_element_by_xpath('//*[@id="mainsrp-pager"]/div/div/div/div[2]/span[3]')
    #这里点击实在没办法了,上网查了一下还可以用js方法
    js = "var q=document.getElementsByClassName('J_Ajax J_Pager link icon-tag').click()"
    browser.execute_script(js)
    browser.refresh()
    get_info(page,depth)
    browser.implicitly_wait(5)

if __name__ == '__main__':
    key_word = input('请输入你想要查询的商品:')
    depth = int(input('请输入你想要查询多少页:'))
    url = 'https://s.taobao.com/search?q=' + quote(key_word)
    browser.get(url)
    sleep(5)
    page = 0
    get_info(page,depth)
    
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值