Home > other >  Climb the baidu search index code, an error SyntaxError: invalid syntax
Climb the baidu search index code, an error SyntaxError: invalid syntax

Time:09-21

The code is as follows,
The import requests
The import sys
The import time

Word_url='http://index.baidu.com/api/SearchApi/thumbnail? Area=0 & amp; The word={} '
COOKIES='


Def decrypt (t, e) :
N=list (t)
I=list (e)
A={}
Result=[]
Ln=int (len (n)/2)
Start=n/ln:
End=n [: ln]
For j, k in zip (start, end) :
A.u pdate ({k: j})
For j in e:
Result. Append (al-qeada et (j))
Return '. Join (result)


Def get_index_home (keyword) :
Headers={
'the user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10 _13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36 ',
'COOKIES' : COOKIES
}
Resp=requests. Get (word_url. The format (keyword), headers=headers)
J=resp. Json ()
Uniqid=j.g et (' data '). Get 'uniqid ()
Return get_ptbk uniqid ()


Def get_ptbk uniqid () :
Url="http://index.baidu.com/Interface/ptbk? Uniqid={} '
Ptbk_headers={
'Accept' : 'application/json, text/plain, */*',
'the Accept - Encoding' : 'gzip, deflate,
'the Accept - Language' : 'useful - CN, useful; Q=0.9 ',
'cache-control:' no - Cache,
'COOKIES' : COOKIES,
'DNT' : '1',
'Host' : 'index.baidu.com',
'Pragma' : 'no - cache',
'Proxy - Connection' : 'keep alive -'
'Referer:' http://index.baidu.com/v2/main/index.html ',
'the user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10 _13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36 ',
'X - Requested - With' : 'the XMLHttpRequest,
}
Resp=requests. Get (url. The format (uniqid), headers=ptbk_headers)
If resp. Status_code!=200:
Print (' failed to get uniqid ')
Sys. Exit (1)
Return resp. Json (). The get (' data ')


Def get_index_data (keyword, start='2011-01-03', end='2019-08-05') :
Url=f 'http://index.baidu.com/api/SearchApi/index? The word={keyword} & amp; Area=0 & amp; StartDate={start} & amp; EndDate={end} '
Headers={
'Accept' : 'application/json, text/plain, */*',
'the Accept - Encoding' : 'gzip, deflate,
'the Accept - Language' : 'useful - CN, useful; Q=0.9 ',
'cache-control:' no - Cache,
'COOKIES' : COOKIES,
'DNT' : '1',
'Host' : 'index.baidu.com',
'Pragma' : 'no - cache',
'Proxy - Connection' : 'keep alive -'
'Referer:' http://index.baidu.com/v2/main/index.html ',
'the user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10 _13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36 ',
'X - Requested - With' : 'the XMLHttpRequest,
}
Resp=requests. Get (url, headers=headers)
If resp. Status_code!=200:
Print (' failed to get index)
Sys. Exit (1)
Data=(https://bbs.csdn.net/topics/resp.json). The get (' data '). The get (' userIndexes) [0]
Uniqid=data. The get (' uniqid ')
PTBK=get_index_home uniqid ()
While PTBK is None or PTBK==":
PTBK=get_index_home uniqid ()
All_data=https://bbs.csdn.net/topics/data.get (' all '). The get (' data ')
Result=decrypt (PTBK all_data)
Result=result. The split (', ')
Print (result)


If __name__=="__main__ ':
Get_index_data (' cool Ann)


There are in the wrong line:


An error is as follows:



Request to help solve the great god, which is to climb baidu search index code,

CodePudding user response:

Grammar is python3.6 f - strings to the above, your python version a little bit low

https://zhuanlan.zhihu.com/p/62774871
  • Related