suger7 发表于 2022-6-20 16:45:55

python获取知乎文章数据

今天我们分享下用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")


zchzzz 发表于 2022-6-23 15:36:40

1123123123

zchzzz 发表于 2022-6-24 09:35:37

123123123uu

jcflkajl 发表于 2022-6-30 17:42:55

谢谢分享!

zchzzz 发表于 2022-8-7 15:34:30

123123123

zchzzz 发表于 2022-8-20 12:31:45

12312321312312321321

zchzzz 发表于 2022-8-24 12:13:31

123123123123

zchzzz 发表于 2022-8-25 12:19:06

12312312312

zchzzz 发表于 2022-8-27 21:57:19

123123123

zchzzz 发表于 2022-8-28 17:04:32

111111111

zchzzz 发表于 2022-8-29 20:41:02

123123123

zchzzz 发表于 2022-11-25 14:20:44

123123123
页: [1]
查看完整版本: python获取知乎文章数据