python 爬蟲入門

來源:互聯網
上載者:User

標籤:指定   open   htm   入門   %s   編碼   script   res   pytho   

import requestsimport re# TODO 下載 每一個小說的首頁url# TODO 大迴圈# 1. 下載小說首頁novel_url = ‘http://www.jingcaiyuedu.com/book/15205/list.html‘response = requests.get(novel_url)# 處理字元編碼 顯式的指定,response.encoding = ‘utf-8‘html = response.text # 字串# print(html)# 2. 提取 章節url 非貪婪匹配title = re.findall(r‘<meta name="keywords" content="《(.*?)》‘,html)[0]# print(title)# id = list dl 有兩個dl = re.findall(r‘<dl id="list">.*?</dl>‘,html)[1]# print(dl)chapter_info_list = re.findall(r‘<a.*?href="(.*?)".*?>(.*?)</a>‘,dl)# print(chapter_info_list)# 資料持久化 寫入txtfb = open(‘%s.txt‘%title,‘w‘,encoding=‘utf-8‘)# 3. 迴圈的去訪問每個章節,提取內容for chapter_info in chapter_info_list:    chapter_url = chapter_info[0]    chapter_title = chapter_info[1]    # 處理 相對url    if ‘http‘ not in chapter_url:        chapter_url = ‘http://www.jingcaiyuedu.com%s‘ % chapter_url    # 下載章節頁面    chapter_response = requests.get(chapter_url)    chapter_response.encoding = "utf-8"    chapter_html = chapter_response.text    # print(chapter_response.text)    # 提取內容    chapter_content = re.findall(r‘<script>a1\(\);</script>(.*?)<script>a2\(\);</script>‘,chapter_html)[0]    # 清洗資料,把多餘的字元處理掉    chapter_content = chapter_content.replace(‘ ‘,‘‘)    chapter_content = chapter_content.replace(‘<br/>‘,‘‘)    chapter_content = chapter_content.replace(‘<br>‘,‘‘)    chapter_content = chapter_content.replace(‘&nbsp;‘,‘‘)    # print(chapter_content)    # 寫入檔案    fb.write(chapter_title)    fb.write(‘\n‘)    fb.write(chapter_content)    fb.write(‘\n‘)    # chapter_response.close()    print(chapter_url)    # exit()

 

python 爬蟲入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.