爬取企查查熱搜

由于是第一次寫作可能代碼風(fēng)格比較丑而且語言表達(dá)不好,各位看官請見諒.

下面進(jìn)入正題臨時(shí)接到一個(gè)任務(wù)爬取企查查的網(wǎng)絡(luò)熱詞,并且要定時(shí)更新. 下面是要爬取的網(wǎng)頁內(nèi)容.

image

之前有寫過這個(gè)頁面的解析代碼,但是事件過的太久已經(jīng)找不到了.有點(diǎn)難受,不過這個(gè)頁面沒有反爬.話不多說直接上代碼



url ='https://www.qichacha.com/cms_topsearch'

ht = requests.get(url=url,headers=headers)

et = etree.HTML(ht.text)

uls = et.xpath('//ul[@class="list-group topsearch-list"][1]/a')

# jinri熱搜

for ulin uls[:51]:

type_ ='今日熱搜'

    search_num = ul.xpath('./span[last()]/text()')[0]

company = ul.xpath('./span[last()-1]/text()')[0]

company_url ='https://www.qichacha.com' + ul.xpath('./@href')[0]

date =str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

print(company, search_num, company_url, date)

cursor = conn.cursor()

sql ='insert into top_search(type_,company,search_num,company_url,sj_time) values(%r,%r,%r,%r,%r)' % (

type_, company, search_num, company_url, date)

cursor.execute(sql)

conn.commit()

uls = et.xpath('//ul[@class="list-group topsearch-list"][1]/a')

# 一周熱搜

for ulin uls[51:101]:

type_ ='一周熱搜'

    search_num = ul.xpath('./span[last()]/text()')[0]

company = ul.xpath('./span[last()-1]/text()')[0]

company_url ='https://www.qichacha.com' + ul.xpath('./@href')[0]

date =str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

print(company, search_num, company_url, date)

cursor = conn.cursor()

sql ='insert into top_search(type_,company,search_num,company_url,sj_time) values(%r,%r,%r,%r,%r)' % (

type_, company, search_num, company_url, date)

cursor.execute(sql)

conn.commit()

uls = et.xpath('//ul[@class="list-group topsearch-list"][1]/a')

# 一月熱搜

for ulin uls[101:]:

type_ ='一月熱搜'

    search_num = ul.xpath('./span[last()]/text()')[0]

company = ul.xpath('./span[last()-1]/text()')[0]

company_url ='https://www.qichacha.com' + ul.xpath('./@href')[0]

date =str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

print(company, search_num, company_url, date)

cursor = conn.cursor()

sql ='insert into top_search(type_,company,search_num,company_url,sj_time) values(%r,%r,%r,%r,%r)' % (

type_, company, search_num, company_url, date)

cursor.execute(sql)

conn.commit()

頁面解析比較簡單,畢竟新手熟悉下流程

然后就是改成定時(shí)任務(wù),我用的是python內(nèi)置庫 schedule


schedule.every(1).minutes.do(job)

schedule.every().hour.do(job)

schedule.every().day.at("10:30").do(job)

schedule.every(5).to(10).days.do(job)

schedule.every().monday.do(job)

schedule.every().wednesday.at("13:15").do(job)

每隔1分鐘執(zhí)行一次任務(wù)

每隔一小時(shí)執(zhí)行一次任務(wù)

每天的10:30執(zhí)行一次任務(wù)

每隔5到10天執(zhí)行一次任務(wù)

每周一的這個(gè)時(shí)候執(zhí)行一次任務(wù)

每周三13:15執(zhí)行一次任務(wù)


def seach():

    schedule.every(20).seconds.do(qcc_reci)

while True:

    schedule.run_pending()

    time.sleep(1)

    seach()

run_pending:運(yùn)行所有可以運(yùn)行的任務(wù)

第一次寫簡書,很多格式不會用.....

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,619評論 1 32
  • ??JavaScript 是一種極其靈活的語言,具有多種使用風(fēng)格。 ??一般來說,編寫 JavaScript 要么...
    霜天曉閱讀 828評論 0 0
  • 弄懂js異步 講異步之前,我們必須掌握一個(gè)基礎(chǔ)知識-event-loop。 我們知道JavaScript的一大特點(diǎn)...
    DCbryant閱讀 2,867評論 0 5
  • 花了幾天時(shí)間,終于看這本書。最近在練習(xí)速讀,但這本書我選擇了慢讀。 收獲滿滿,律師屬專業(yè)顧問服務(wù)提供者,此書的觀點(diǎn)...
    法稅筆記閱讀 7,503評論 -1 1
  • 文/Pinky 今天是2018年最后一天,從明天開始,2018就離我們一天比一天遙遠(yuǎn)了。 以前小的時(shí)候特別盼著19...
    記憶的瓶子Pinky閱讀 330評論 2 7

友情鏈接更多精彩內(nèi)容