随笔 - 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

今天我们分享下用Python爬取知乎前100用户的数据,选择对象是知乎排行榜前100用户,各自按访问量从高到低排序爬取他们个人主页的所有文章数据,包括赞同数、收藏数、标题等等,使用一些简单的数据分析手段看看技术。
首先我们打开知乎的个人主页,利用requests分析下网站,简单的分析后发现单纯的访问不带cookie返回的是空页面,而且多次访问后会被封IP,所以我们需要获取数据爬虫程序里面就需要添加上cookie和代理IP,最好是ua一起加上。代理的选择这里推荐亿牛云爬虫代理IP,IP池大,延迟低,速度快,对于有一定反爬机制的网站是最好的选择。访问过知乎的都知道,是需要先登录才能看到内容的,所以这里我们选择使用Selenium模拟浏览器的方式进行登录操作。在获取数据过程加上代理获取数据的实现过程如下:
 

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  阅读(504)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示