新手,在学习python爬虫,环境是python3.4,想爬取人民日报评论员文章,现在只怕去了一个网页,代码如下,
import requests
from bs4 import BeautifulSoup
import re
myUrl = "http://cpc.people.com.cn/pinglun/n1/201/0613/c78779-28428425.html"
response = requests.get(myUrl)
soup = BeautifulSoup(response.text, "lxml", from_encoding="gbk")
print(soup.title.string.encode('ISO-8859-1').decode('gbk'))
for a in soup.find_all(style="text-indent: 2em;"):
print(a.string.encode('ISO-8859-1').decode('gbk'))
网页上出错的源代码如下:
<span style="text-indent: 2em; display: block;" id="paper_num">《 人民日报 》( 2016年06月13日 01 版)</span>
我的出错提示如下:
Traceback (most recent call last):
File "pa_chong_lx.py", line 21, in <module>
print(a.string.encode('ISO-8859-1').decode('gbk'))
AttributeError: 'NoneType' object has no attribute 'encode'
原因分析:
我查找的关键词是style="text-indent: 2em;,这段代码<span style="text-indent: 2em; display: block;" id="paper_num">《 人民日报 》( 2016年06月13日 01 版)</span> 格式与前边的主题文章代码不一样,所以出错,求解答怎么改。
新手,因为编码的问题卡了好久,感觉一步一个坑,步步是坑!python虽然简单,但也正是简单,我不知道哪里出错了,或者是知道错误但不知道怎么改正。
原來程式碼中的連結已經失效,我以 http://cpc.people.com.cn/n1/2016/0628/c404684-28502214.html 中文章為範例。
可以正常運作的程式碼:
運行結果:
這裡遇到的編碼問題很常見,簡單來說就是 requests 猜錯了網頁的編碼方式。
requests 取得response 後,會根據 headers 中給出的編碼來解碼拿到的數據,如果響應 header 沒有指定編碼,則預設指定為 ISO-8859-1(encoding 屬性)。還好 requests 還可以根據內容猜測編碼方案,推測的結果保存在 apparent_encoding 屬性中,針對人民日報評論,這裡是 GB2312。所以,只需要製定 encoding = apparent_encoding,然後取得text 即可得到正確的解碼結果。 (注意apparent_encoding並不能保證 100%正確)
requests 文件部分可以參考Response Content
關於編碼的理解,可以參考:人機互動之字符編碼和 五分鐘戰勝 Python 字符編碼。
關於requests 編碼解析的詳細內容,參考Python + Requests 編碼問題
編碼確實是個坑,不過搞清楚了,就很容易避過去。
找到一個公共的元素,然後用正規來篩選資料吧
報錯原因NoneType類別沒有encode屬性,表示你用soup.find_all()沒有匹配到括號內的參數,你試試先匹配一下tag,再匹配style,可能會找到原因,實在不行上正則