在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/ Python問答
敢試 回答

user.objects.last()
可以得到最后一條數(shù)據(jù),如果沒有在model指定ordering, 就會(huì)按照默認(rèn)排序取最后一條,默認(rèn)一般是用pk做排序。

柚稚 回答

這樣的話是可以的

  • 不使用x參數(shù)
class A:

    def __init__(self, x):
        self.x = x

    def a(self, a):
        return print(a)

    def b(self, b):
        return print(b)

A = A(object)
print(A.a(5))
  • 使用x參數(shù)

class A:

    def __init__(self, x):
        self.x = x

    def a(self, a):
        return print(a)

    def b(self):
        return print(self.x)

B = A(3)
print(B.b())
孤毒 回答

403 Forbidden 錯(cuò)誤,大多是被服務(wù)器屏蔽了,拒絕提供返回內(nèi)容

一般可以通過更換服務(wù)器ip、設(shè)置代理服務(wù)器,去爬取

最好的辦法,是通過模擬瀏覽器人工采集爬取

selenium + xvfb + firefox + proxy ip

下面是我的解決方案,僅供參考,相互學(xué)習(xí)

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.proxy import *
from pyvirtualdisplay import Display
# from xvfbwrapper import Xvfb

import bs4, os
from base64 import b64encode

import sys
reload(sys)
sys.setdefaultencoding('utf8')


## webdriver + firefox (不使用代理,爬取網(wǎng)頁)
def spider_url_firefox(url):
    browser = None
    display = None
    try:
        display = Display(visible=0, size=(800, 600))
        display.start()
        browser = webdriver.Firefox()       # 打開 FireFox 瀏覽器
        browser.get(url)     
        content = browser.page_source
        print("content: " + str(content))
    finally:
        if browser: browser.quit()
        if display: display.stop()


## webdriver + firefox + proxy + whiteip (無密碼,或白名單ip授權(quán))
## 米撲代理:https://proxy.mimvp.com
def spider_url_firefox_by_whiteip(url):
    browser = None
    display = None
    
    ## 白名單ip,請(qǐng)見米撲代理會(huì)員中心: https://proxy.mimvp.com/usercenter/userinfo.php?p=whiteip
    mimvp_proxy = { 
                    'ip'            : '140.143.62.84',      # ip
                    'port_https'    : 19480,                # http, https
                    'port_socks'    : 19481,                # socks5
                    'username'      : 'mimvp-user',
                    'password'      : 'mimvp-pass'
                  }
    
    try:
        display = Display(visible=0, size=(800, 600))
        display.start()
        
        profile = webdriver.FirefoxProfile()
        
        # add proxy
        profile.set_preference('network.proxy.type', 1)     # ProxyType.MANUAL = 1
        if url.startswith("http://"):
            profile.set_preference('network.proxy.http', mimvp_proxy['ip'])
            profile.set_preference('network.proxy.http_port', mimvp_proxy['port_https'])    # 訪問http網(wǎng)站
        elif url.startswith("https://"):
            profile.set_preference('network.proxy.ssl', mimvp_proxy['ip'])
            profile.set_preference('network.proxy.ssl_port', mimvp_proxy['port_https'])     # 訪問https網(wǎng)站
        else:
            profile.set_preference('network.proxy.socks', mimvp_proxy['ip'])
            profile.set_preference('network.proxy.socks_port', mimvp_proxy['port_socks'])
            profile.set_preference('network.proxy.ftp', mimvp_proxy['ip'])
            profile.set_preference('network.proxy.ftp_port', mimvp_proxy['port_https'])
            profile.set_preference('network.proxy.no_proxies_on', 'localhost,127.0.0.1')
        
        ## 不存在此用法,不能這么設(shè)置用戶名密碼 (舍棄)
#         profile.set_preference("network.proxy.username", 'mimvp-user')
#         profile.set_preference("network.proxy.password", 'mimvp-pass')
    
        profile.update_preferences()
        
        browser = webdriver.Firefox(profile)       # 打開 FireFox 瀏覽器
        browser.get(url)     
        content = browser.page_source
        print("content: " + str(content))
    finally:
        if browser: browser.quit()
        if display: display.stop()
心癌 回答

因?yàn)槭?code>__init__而不是__init哈哈哈哈哈哈哈哈哈哈哈哈哈哈你少了兩個(gè)下劃線嚴(yán)肅

萌二代 回答

你的代碼不止這些吧,能否貼全,這樣才能判斷問題的所在

神曲 回答

對(duì)于列表L的切片L[start: stop: step]:
如果step>0,L[: : step]等價(jià)于L[0: len(L): step];
如果step<0,L[: : step]等價(jià)于L[-1: -1 - len(L): step].

瘋子范 回答

由于request.url是只讀的屬性,所以不能直接更改。
但看了一下源碼,可以嘗試在下載器中間件中這樣寫:

def process_request(self, request, spider):
    request._set_url(request.url + '&t=%s' % self.gettime())
臭榴蓮 回答

你這是scrapy框架嗎?這是有優(yōu)先級(jí),越小越優(yōu)先。

舊言 回答

5次?不是7次嗎.

clipboard.png
你直接for循環(huán)了conMidtab2下所有的城市,但是沒有注意到頁面上的天氣不只是一天.

離魂曲 回答

PEP 342: finally 一定會(huì)被執(zhí)行,無論生成器是否執(zhí)行結(jié)束,finally實(shí)際是被garbage collection執(zhí)行的。所以你直接af().next()時(shí),af()是個(gè)臨時(shí)對(duì)象,會(huì)在print調(diào)用之前先被gc回收,自然先出現(xiàn)end,而g.next()則是正常的方式,g在主程序結(jié)束前始終存在,所以end出現(xiàn)在程序運(yùn)行結(jié)束之后,即python在主程序運(yùn)行結(jié)束后運(yùn)行g(shù)c才會(huì)執(zhí)行finally里面的內(nèi)容

import datetime

date_publish = models.IntegerField(verbose_name="更新時(shí)間戳" , default=datetime.datetime.now)

影魅 回答

錯(cuò)誤不在第 17 行, 錯(cuò)誤在第 15 行. 你少寫一個(gè)括號(hào).

毀了心 回答

我遇到過一次中文路徑的問題,是通過在uwsgi.ini配置中修改Linux的語言環(huán)境解決的

[uwsgi]
...
env=DJANGO_SETTINGS_MODULE=mysite.settings # 解決中文編碼異常問題
env=LC_ALL=zh_CN.UTF-8

可以參考一下

愛礙唉 回答

使用python的collections

import json
from collections import defaultdict


def main():
    ret = defaultdict(list)
    data = [
     {'_id': '5abb4f9ca7e2c54c757b3e48',
      'amount': 8400,
      'buyerEmail': 'otzYzwMh24edWk8NxSJOqCSZREe0',
      'from': 'weixin',
      'orderid': '2018032816173212079',
      'real': 8400,
      'status': 1,
      'tradeNo': '4200000099201803287230332578',
      'uid': '5abb36051a62067bf7e30178' # 需要把ObjectId處理成字符串
    },
    {'_id': '5b6699f6df03ec3294d7c0a4',
      'amount': 100,
      'buyerEmail': 'otzYzwC3YwRdu7QrWLXqS3VRJybI',
      'from': 'weixin',
      'orderid': '2018080514322245193',
      'real': 100,
      'status': 1,
      'tradeNo': '4200000148201808052403940202',
      'uid': '5a5738411a62061972e128cb' # 需要把ObjectId處理成字符串
    },
    {'_id': '5b6699f6df03ec3294d7c0a4',
      'amount': 200,
      'buyerEmail': 'otzYzwC3YwRdu7QrWLXqS3VRJybI',
      'from': 'weixin',
      'orderid': '2018080514322245193',
      'real': 200,
      'status': 1,
      'tradeNo': '4200000148201808052403940202',
      'uid': '5a5738411a62061972e128cb' # 需要把ObjectId處理成字符串
    },
    ]
    
    for d in data:
        ret[d.get('uid')].append(d)
        
    print(json.dumps(ret, indent=2))
if __name__ == '__main__':
    main()
    
法克魷 回答

字符串索引必須為整數(shù),而不是字符。
你的問題在于requests.get()函數(shù)的cookies參數(shù),必須為一個(gè)字典或cookiejar對(duì)象,而你卻賦值了一個(gè)字符串。

念初 回答

最好的辦法是來個(gè)小例子試一下,

假設(shè)你有一個(gè)data.cvs的逗號(hào)分隔的數(shù)據(jù)文件,內(nèi)容如下

0     index,name,comment,,,,
1    1,name_01,coment_01,,,,
2    2,name_02,coment_02,,,,
3    3,name_03,coment_03,,,,
4    4,name_04,coment_04,,,,
5    5,name_05,coment_05,,,,

用下面的代碼來讀

import pandas as pd
word = pd.read_table('data.csv', delimiter=',',encoding = 'utf-8', names = ['index','name','comment','foo','bar','baz'], header=0)

print(word)

你將看到如下的結(jié)果:

      index       name  comment  foo  bar  baz
1   name_01  coment_01      NaN  NaN  NaN  NaN
2   name_02  coment_02      NaN  NaN  NaN  NaN
3   name_03  coment_03      NaN  NaN  NaN  NaN
4   name_04  coment_04      NaN  NaN  NaN  NaN
5   name_05  coment_05      NaN  NaN  NaN  NaN
......

回答你的問題:names是指讀到內(nèi)存后的數(shù)據(jù)的列名,heads是指數(shù)據(jù)表頭行號(hào),真正的數(shù)據(jù)是這一行之后開始。

笨笨噠 回答

你下載的djongo里沒有中文語言包。

萢萢糖 回答

報(bào)錯(cuò)是編碼錯(cuò)誤

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 10-21: Body ('禁限行令讓二手車更難出手') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

按提示 改成這樣試試

json.dumps(data).encode('utf-8')
孤影 回答

像stomp + rabbitmq, 我覺得已經(jīng)夠簡單了(http://jmesnil.net/stomp-webs...)。
雖然偷懶是人類進(jìn)步的動(dòng)力,但是該花的精力還得花,至少能帶來穩(wěn)定性和效率上的好處。