user.objects.last()
可以得到最后一條數(shù)據(jù),如果沒有在model指定ordering, 就會(huì)按照默認(rèn)排序取最后一條,默認(rèn)一般是用pk做排序。
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))
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次嗎.
你直接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里沒有中文語言包。
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)定性和效率上的好處。
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進(jìn)“中國制造2025”,實(shí)現(xiàn)中華民族偉大復(fù)興的升級(jí)產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。