Python網路爬蟲(七):百度文庫文章爬取器__Python

來源:互聯網
上載者:User

在用前面的方法爬取百度文庫的文章時,發現只能爬取已顯示出來的幾頁文章,而對於沒有顯示的頁數則無法獲得其內容。如果要完整的看到整篇文章,需要手動地點擊底下的“繼續閱讀”,使所有的頁數都顯示出來。

查看元素後發現,展開前的html和展開後的html是不同的,前者隱藏頁的的常值內容是沒有顯示的。但是爬蟲獲得的是展開前的html檔案,所以也就只能獲得部分內容。
本文使用了一個工具來自動化操作網頁,獲得展開後的html。 使用Selenium自動化工具來操控瀏覽器 Selenium的安裝
pip3 install Selenium 安裝chromedriver.exe
這裡踩了很多坑。
驅動下載地址:
http://chromedriver.storage.googleapis.com/index.html
一定要下載與chrome版本相匹配的chromedriver,而且注意並不是版本號碼越大的驅動對應最新的chrome瀏覽器,要仔細查看notes.txt檔案看對應關係。比如我的chrome是v62,支援的chromedriver是v2.33。 將安裝程式拖到C:\Program Files (x86)\Google\Chrome\Application\目錄下 設定環境變數:win+r,輸入sysdm.cpl,進階,環境變數,設定Path為C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe。或者在調用chrome時指定這個路徑。
browser = webdriver.Chrome(‘C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe’) 使用selenium自動操作網頁:

from selenium import webdriveroptions = webdriver.ChromeOptions()options.add_argument('user-agent="Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"')driver = webdriver.Chrome(chrome_options=options)driver.get('https://www.baidu.com/')html = driver.page_source
完整代碼
# contents_bdwk.pyfrom selenium import webdriverfrom bs4 import BeautifulSoup# ***selenium 自動操作網頁***options = webdriver.ChromeOptions()options.add_argument('user-agent="Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36"')   #設定裝置代理程式driver = webdriver.Chrome(chrome_options=options)driver.get('https://wenku.baidu.com/view/aa31a84bcf84b9d528ea7a2c.html')    #此處填寫文章地址page = driver.find_element_by_xpath("//div[@id='html-reader-go-more']")driver.execute_script('arguments[0].scrollIntoView();', page)               #拖動網頁到可見的元素去nextpage = driver.find_element_by_xpath("//span[@class='moreBtn goBtn']")nextpage.click()                                                            #進行點擊下一頁操作# ***對開啟的html進行分析***html = driver.page_sourcebf1 = BeautifulSoup(html, 'lxml')# 獲得文章標題title = bf1.find_all('h1', class_='reader_ab_test with-top-banner')bf2 = BeautifulSoup(str(title), 'lxml')title = bf2.find('span')title = title.get_text()filename = title + '.txt'# 獲得文章內容texts_list = []result = bf1.find_all('div', class_='ie-fix')for each_result in result:    bf3 = BeautifulSoup(str(each_result), 'lxml')    texts = bf3.find_all('p')    for each_text in texts:        texts_list.append(each_text.string)contents = ''.join(texts_list).replace('\xa0', '')# ***儲存為.txt檔案with open(filename, 'a', encoding='utf-8') as f:    f.writelines(contents)    f.write('\n\n')
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.