import re import csv from bs4 import BeautifulSoup from selenium.webdriver import Chrome import requests def page_open(str1): b = Chrome() b.get(str1) tag = b.find_element_by_id('key') tag.send_keys('勋章\n') b.switch_to.window(b.window_handles[-1]) for x in range(100): b.execute_script('window.scrollBy(0,1000)') return b def page_over(b): over = b.find_element_by_class_name('.pn-next') over.click() b.switch_to.window(b.window_handles[-1]) return b def data(b): h1 = b.page_source soup = BeautifulSoup(h1, 'lxml') div1 = soup.select('#J_goodsList') all_data = [] for x in div1[1:len(div1):1]: names = x.select('.p-name p-name-type-2>a').attrs['title'] prices = x.select('.p-price>strong i').text comment = x.select('.p-commit a').text shops = x.select('.p-shop>.J_im_icon>.curr-shop hd-shopname').text if x.select('.p-icons>i').text != None: icons = x.select('.p-icons>i').text else: icons = '' all_data.append([names,prices,comment,shops,icons]) writer.writerows(all_data) writer = csv.writer(open('files/勋章.csv', 'wt', encoding='utf-8', newline='')) writer.writerow(['名称', '价格', '评论数', '店铺名', '标示']) str1='https://www.jd.com/' b=page_open(str1) for x in range(99): data(b) page_over(b) data(b)
day18hw
最新推荐文章于 2024-08-22 22:01:15 发布