小编Sum*_*Jha的帖子

Python中的Selenium PhantomJS自定义标头

我想在python中向Selenium PhantomJS添加"自定义标题".这些是我想要添加的标题.

headers = { 'Accept':'*/*',
            'Accept-Encoding':'gzip, deflate, sdch',
            'Accept-Language':'en-US,en;q=0.8',
            'Cache-Control':'max-age=0',
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'
          }
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的代码:

from selenium import webdriver

service_args = [
    '--proxy=127.0.0.1:9999',
    '--proxy-type=socks5',
    ]
driver = webdriver.PhantomJS(service_args=service_args)


driver.set_window_size(1120, 550)
driver.get("https://duckduckgo.com/")
driver.find_element_by_id('search_form_input_homepage').send_keys("realpython")
driver.find_element_by_id("search_button_homepage").click()
print driver.current_url
driver.quit()
Run Code Online (Sandbox Code Playgroud)

如何修改包含这些自定义标头的代码?

请帮忙.

python selenium custom-headers phantomjs

13
推荐指数
2
解决办法
2万
查看次数

shouldShowRequestPermissionRationale和requestPermissions有什么区别?

我正在构建一个需要用户位置的应用程序.我正在关注此处的Android培训文档,其中说:

shouldShowRequestPermissionRationale 返回布尔值,指示我们是否应该显示具有请求权限的理由的UI(危险权限,ACCESS_FINE_LOCATION)

现在在这段代码中(取自文档本身):

if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback …
Run Code Online (Sandbox Code Playgroud)

android android-location android-permissions

11
推荐指数
1
解决办法
7649
查看次数

用于基于类的通用视图的 Django mixins

我正在尝试实现 staff_member_required 混合:

以下是我找到的两种方法:

第一的:

class StaffRequiredMixin(object):
    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_staff:
            messages.error(
                request,
                'You do not have the permission required to perform the '
                'requested operation.')
            return redirect(settings.LOGIN_URL)
        return super(StaffRequiredMixin, self).dispatch(request,
            *args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

第二:

class StaffRequiredMixin(object):
    @classmethod
    def as_view(self, *args, **kwargs):
        view = super(StaffRequiredMixin, self).as_view(*args, **kwargs)
        return staff_member_required(view)

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_staff:
            messages.error(
                request,
                'You do not have the permission required to perform the '
                'requested operation.')
            return redirect(settings.LOGIN_URL)
        return …
Run Code Online (Sandbox Code Playgroud)

django mixins django-views

4
推荐指数
1
解决办法
668
查看次数