Python小白的爬蟲問題與解決(含程式碼)
因為上一篇文章沒辦法放程式碼,所以重新發一次
2018/5/7
看到作者發的程式碼很簡單,嗯,適合我這個新手+懶人!但是,問題沒有那麼簡單!
原文地址:https://www.jianshu.com/p/ea0b56e3bd86

抓取1頁影評,print內容出錯

抓取結果編碼出錯,存在score未取到的情況
Python版本:2.7.13
2018/5/8

print同時列印2個結果時,文字編碼錯誤

print結果參考,求大神告知原因

獲取使用者名稱程式碼,xpath內容待學習

測試結果OK

列印全部內容

列印結果OK
5/9

檢視列表元素的型別

評分獲取程式碼

原始碼

程式碼執行結果,先獲取class屬性儲存為字串,取字串的第8位即所需評分

列表內容獲取正常

嘗試寫入CSV出錯,發現name中既有Unicode又有str,絕望!!
參考前輩經驗後,編碼問題得到改善。
連結:https://blog.csdn.net/gyafdxis/article/details/77923516

文字能夠正常顯示,但是不在同一個單元格

萬能百度:.writerow()和writerows()寫入的是list型別,把單個資料變成[]就行。

剛開心一會,又碰到了問題!!我要換Python3(;´д`)ゞ
參考了這位大神(https://www.jianshu.com/p/d1bf2f0bdc51)對資料的儲存方法,終於成功了!!

3個元素作為一個列表儲存到大列表中

將列表中的資料依次寫入CSV

終於成功啦!撒花!
5/10
今日任務:把程式碼改為函式

這一段程式碼導致程式執行無結果,連print都不行,只能重新處理編碼問題

在經歷了無數次報錯,無數次百度後終於找到了問題的關鍵

把資料統一為‘str’類,最終執行成功

執行中Tqdm出現警告,不知道什麼原因,不過對結果沒有影響
最後修改完成的程式碼
import requests
from lxml import etree
import pandas as pd
import time
import random
from tqdm import tqdm
import csv
import codecs
'''
import sys
reload(sys)
sys.setdefaultencoding('utf8')
'''
data = []
def getyp(page):
url = 'https://movie.douban.com/subject/6390825/comments?start=%d&limit=20&sort=new_score&status=P&percent_type='%(page*20)
response = requests.get(url)
response.encoding = 'utf-8'
#print (response.content)
response = etree.HTML(response.content)
print (url)
for i in range(1,21):#每頁顯示20條評論
name1 = response.xpath('//*[@id="comments"]/div[%d]/div[2]/h3/span[2]/a'%(i))#獲取使用者名稱,儲存為列表形式,每次迴圈,name1中都只有一個元素
score1 = response.xpath('//*[@id="comments"]/div[%d]/div[2]/h3/span[2]/span[2]'%(i))#獲取評分
comment1 = response.xpath('//*[@id="comments"]/div[%d]/div[2]/p'%(i))#獲取影評
if type(name1[0].text) == unicode:
name_element = name1[0].text.encode('utf-8')
else:
name_element = name1[0].text
score_element = score1[0].attrib['class'][7]#獲取class屬性,取第8個字元
if type(comment1[0].text) == unicode:
comment_element = comment1[0].text.encode('utf-8')
else:
comment_element = comment1[0].text
print type(comment_element)
data.append([name_element,score_element,comment_element])
for i in tqdm(range(1,3)):#抓取2頁
getyp(i)
time.sleep(random.uniform(6,9))
with open("DBtest.csv","wb") as f:
f.write(codecs.BOM_UTF8)
writer = csv.writer(f)
writer.writerow(['name','score','comment'])
for k in data:
writer.writerow(k)
相關文章
- python爬蟲爬取網頁中文亂碼問題的解決Python爬蟲網頁
- Python爬蟲程式設計常見問題解決方法Python爬蟲程式設計
- Python爬蟲亂碼問題Python爬蟲
- 爬蟲常見問題及解決方式爬蟲
- 新手小白的爬蟲神器-無程式碼高效爬取資料爬蟲
- Selenium爬蟲遇到超時TimeOut問題的解決方法爬蟲
- 如何利用ip住宅代理解決python爬蟲遇到反爬措施的問題?Python爬蟲
- python爬蟲常見的那點問題!Python爬蟲
- Java爬蟲與Python爬蟲的區別?Java爬蟲Python
- 爬蟲常見錯誤程式碼及解決措施爬蟲
- Python 萬能程式碼模版:爬蟲程式碼篇Python爬蟲
- 小白學 Python 爬蟲(25):爬取股票資訊Python爬蟲
- python爬蟲總是爬不到資料,你需要解決反爬蟲了Python爬蟲
- 手把手教你寫網路爬蟲(8):徹底解決亂碼問題爬蟲
- 使用代理爬蟲出302錯誤程式碼解決方法爬蟲
- Python開發爬蟲專案+程式碼Python爬蟲
- 爬蟲錯誤程式碼如何解決?爬蟲
- 爬蟲ip代理池搭建前需解決的問題及搭建思路爬蟲
- 爬蟲必備工具,掌握它就解決了一半的問題爬蟲
- IPIDEA乾貨|Java爬蟲與Python爬蟲的區別IdeaJava爬蟲Python
- python爬蟲如何爬知乎的話題?Python爬蟲
- 不踩坑的Python爬蟲:Python爬蟲開發與專案實戰,從爬蟲入門 PythonPython爬蟲
- 送給Python小白學習爬蟲的小專案Python爬蟲
- Python爬蟲與Java爬蟲有何區別?Python爬蟲Java
- 解決:如何將Python程式碼打包成exe可執行程式的問題Python行程
- python爬蟲解決趕集網掃碼獲取手機號Python爬蟲
- 徹底解決Python編碼問題Python
- 【Python學習】爬蟲爬蟲爬蟲爬蟲~Python爬蟲
- 手把手教你利用爬蟲爬網頁(Python程式碼)爬蟲網頁Python
- 不用寫程式碼的爬蟲爬蟲
- python爬蟲之多執行緒、多程式+程式碼示例Python爬蟲執行緒
- python解決反爬蟲方法的優缺點對比Python爬蟲
- 爬蟲中經常出現Traceback (most recent call last):問題解決!!!爬蟲AST
- ts程式碼提示很慢問題解決
- 如何爬取視訊的爬蟲程式碼原始碼爬蟲原始碼
- 關於爬蟲工具 colly 的問題爬蟲
- 爬蟲過程中遇到的問題爬蟲
- 爬蟲使用海外HTTP代理時常見的錯誤程式碼及解決方法爬蟲HTTP