1、登录
- 在页面中找到登录按键,使用selenium模拟点击
button_login = wd.find_element(By.XPATH,
"//li[@id='J_SiteNavMytaobao']/div[@class='site-nav-menu-hd']/a[@target='_top']")
button_login.click()
button_username = wd.find_element(By.XPATH, "//div[@class='input-plain-wrap input-wrap-loginid ']/input")
button_username.send_keys(zhangmi.username)
button_password = wd.find_element(By.XPATH, "//div[@class='input-plain-wrap input-wrap-password']/input")
button_password.send_keys(zhangmi.password)
- 滑动进行验证,使用selenium模拟鼠标容易失败,所以我们在这里多等待一段时间手动滑动验证
2、搜索并找到数据
- 找到搜索框输入所需要的物品,这里我们使用input动态输入
- 通过使用开发者工具找到商品所在位置,发现为由a标签组成
info = wd.find_elements(By.XPATH, "//div[@class='content--CUnfXXxv']/div/a")
3、解析数据
messages_title = message.find_element(By.CSS_SELECTOR, ".title--F6pvp_RZ ").text
messages_features = []
messages_feature = message.find_elements(By.CSS_SELECTOR, ".text--eAiSCa_r")
for k in messages_feature:
messages_features.append(k.text)
messages_places = []
messages_place = message.find_elements(By.CSS_SELECTOR, ".procity--QyzqB59i")
for a in messages_place:
messages_places.append(a.text)
messages_url = message.get_attribute("href")
messages_shop = message.find_element(By.CSS_SELECTOR, ".shopNameText--APRH8pWb").text
4、整理并储存数据
info = {
"标题": messages_title,
"特点": messages_features,
"链接": messages_url,
"商家信息": messages_shop,
"商品发货地": messages_places
}
with open("./files/淘宝商品数据.txt", "a", encoding="utf-8") as f:
f.write(str(info) + "\n")