随笔 - 82  文章 - 2 评论 - 1 阅读 - 29061
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

据调研机构公开数据显示,苹果平板电脑2022年第一季度在中国大陆市场出货量为120万台,市场份额达到26%,位居首位,不过出货量同比下降了29%,但是目前还是排名第一。目前国产平板电脑配置,价格对比苹果iPad来说有优势,这也是苹果iPad市场份额逐渐下滑的原因。多多支持国产!

今天我们就使用大数据来分析下,目前市场上各个品牌平板电脑的销量是怎么样的?这里我们的数据来源于京东https://www.jd.com/。但是由于京东反爬技术较强,使用常规方法爬取其数据行不通,所以本文直接使用selenium爬取京东商品数据。使用selenium也需要加上代理IP的辅助才能更好的获取数据,代理IP的选择这里推荐大家使用亿牛云代理https://www.16yun.cn/,他们家提供的爬虫代理效果非常的好。获取商品具体数据完整代码如下:

from selenium import webdriver
import string
import zipfile

# 代理服务器(产品官网 www.16yun.cn)
proxyHost = "t.16yun.cn"
proxyPort = "3111"

# 代理验证信息
proxyUser = "username"
proxyPass = "password"


def create_proxy_auth_extension(proxy_host, proxy_port,
proxy_username, proxy_password,
scheme='http', plugin_path=None):
if plugin_path is None:
plugin_path = r'/tmp/{}_{}@t.16yun.zip'.format(proxy_username, proxy_password)

manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "16YUN Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""

background_js = string.Template(
"""
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "${scheme}",
host: "${host}",
port: parseInt(${port})
},
bypassList: ["localhost"]
}
};

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
return {
authCredentials: {
username: "${username}",
password: "${password}"
}
};
}

chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
"""
).substitute(
host=proxy_host,
port=proxy_port,
username=proxy_username,
password=proxy_password,
scheme=scheme,
)
print(background_js)

with zipfile.ZipFile(plugin_path, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)

return plugin_path


proxy_auth_plugin_path = create_proxy_auth_extension(
proxy_host=proxyHost,
proxy_port=proxyPort,
proxy_username=proxyUser,
proxy_password=proxyPass)

option = webdriver.ChromeOptions()

option.add_argument("--start-maximized")

# 如报错 chrome-extensions
# option.add_argument("--disable-extensions")

option.add_extension(proxy_auth_plugin_path)

# 关闭webdriver的一些标志
# option.add_experimental_option('excludeSwitches', ['enable-automation'])

driver = webdriver.Chrome(
chrome_options=option,
executable_path="./chromdriver"
)

# 修改webdriver get属性
# script = '''
# Object.defineProperty(navigator, 'webdriver', {
# get: () => undefined
# })
# '''
# driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": script})


driver.get("https://httpbin.org/ip")

经常爬虫的朋友都知道爬取到的源数据存在很多脏数据,若要用于统计分析或更深层的分析,必须先进行数据清洗,清洗分析后的数据我们下次在进行分享,敬请期待!
 
 
 
 
posted on   小橙子11  阅读(93)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示