关注
也来凑个热闹吧,直接 python + selenium + chrome 来抓的,下面是结果+代码
#coding=utf-8
# 测试运行环境: ubuntu 14.04 python 2.7.6 chrome
# 需要安装 selenim chromedriver
from selenium import webdriver
from selenium.webdriver.common.by import By
import sys
GBK = 'gbk'
def logToFile(tag, msg):
logFile = open('log', 'w')
out = tag + '--\n' + msg
logFile.write(out)
def log(tag, msg):
print tag, ' -- '
print msg
def defLog(msg):
log('out', msg)
# 保存零食信息
class Item:
def __init__(self):
self.CODE = 'utf-8'
# 输出内容到markdown文件中
class MarkdownWriter:
def __init__(self, name='out.md'):
mdFile = open(name, 'w')
self.mdFile = mdFile
def writeContent(self, content):
self.mdFile.write(content)
def writeItems(self, title, items):
# 组装markdown格式
content = '### ' + title + ' \n'
for item in items:
content += '#### ' + item.title + ' \n'
content += ' \n'
content += '[goto](' + item.url + ') \n'
content += 'money: ' + item.money + ' \n'
content += 'store: ' + item.store + ' \n'
content += '\n\n'
self.mdFile.write(content)
class TaoBaoSpider:
def __init__(self):
driver = webdriver.Chrome()
self.driver = driver
def getUrl(self, url):
print 'start get ...'
# 通过chrome加载url包括js脚本
self.driver.get(url)
print 'get finished ...'
def getHtmlWithJs(self):
return self.driver.page_source
def getElements(self):
print 'get item ...'
els = self.driver.find_elements(By.CSS_SELECTOR, "li[class=' item item-border'")
return els
def getContent(self, element):
item = Item()
# 从获取的html页面中获取需要的信息并封装到item类中
item.img = element.find_element_by_tag_name('img').get_attribute('src')
item.money = element.find_element(By.CSS_SELECTOR, "div[class='price child-component clearfix'").find_element_by_tag_name('strong').text
titleElement = element.find_element(By.CSS_SELECTOR, "div[class='title child-component'").find_element_by_class_name('J_AtpLog')
item.title = titleElement.text
item.url = titleElement.get_attribute('href')
item.store = element.find_element(By.CSS_SELECTOR, "div[class='seller child-component clearfix'").find_element_by_tag_name('a').text
return item
def start(self, url):
self.url = url
self.getUrl(url)
els = self.getElements()
items = []
for e in els:
item = self.getContent(e)
items.append(item)
return items
def main():
# 设置下编码
reload(sys)
sys.setdefaultencoding('utf-8')
url = 'https://world.taobao.com/search/search.htm?_ksTS=1478358034370_312&spm=a21bp.7806943.20151106.1&search_type=0&_input_charset=utf-8&navigator=all&json=on&q=%E5%A5%B3%E6%9C%8B%E5%8F%8B%20%E9%9B%B6%E9%A3%9F&cna=Eg9NDplivkkCAXuCB323%2Fsy9&callback=__jsonp_cb&abtest=_AB-LR517-LR854-LR895-PR517-PR854-PR895'
# 爬虫运行
spider = TaoBaoSpider()
items = spider.start(url)
# 输出到markdown文件中
writer = MarkdownWriter('taobao.md')
writer.writeItems('零食列表', items)
main()
更具体些的可以上这里看:https://github.com/5A59/Practice
查看原帖
点赞 1
相关推荐


影石Insta360
| 校招
| 超多精选岗位
点赞 评论 收藏
分享


柠檬微趣
| 校招
| 1个岗位
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 如何KTV领导 #
30707次浏览 245人参与
# 研究所笔面经互助 #
55006次浏览 394人参与
# 掌阅春招 #
88398次浏览 511人参与
# 软开人,秋招你打算投哪些公司呢 #
66746次浏览 713人参与
# 生物制药/化工校招攻略 #
33707次浏览 264人参与
# 软件开发春招备战日记 #
57412次浏览 490人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
72294次浏览 535人参与
# 你最近一次加班是什么时候? #
31622次浏览 249人参与
# vivo求职进展汇总 #
167779次浏览 1020人参与
# 如何缓解入职前的焦虑 #
141464次浏览 1125人参与
# 产品每日一题 #
28975次浏览 404人参与
# 你遇到过哪些神仙同事 #
44969次浏览 421人参与
# 你今年的平均薪资是多少? #
94111次浏览 460人参与
# 考研人,我有话说 #
13746次浏览 263人参与
# TP-LINK工作体验 #
38451次浏览 786人参与
# 还记得你第一次面试吗? #
75698次浏览 1093人参与
# 想给25届机械人的秋招建议 #
22444次浏览 202人参与
# 上班苦还是上学苦呢? #
201194次浏览 1233人参与
# 在职场上,你最讨厌什么样的同事 #
10580次浏览 123人参与
# 职场上哪些行为很加分? #
31897次浏览 738人参与
# 985本硕1个中小厂offer,摆烂or继续努力 #
100249次浏览 696人参与