python爬蟲常見問題(一)
Python爬蟲所見問題集合:
1. NotImplementedError: Only the following pseudo-classes are implemented: nth-of-type
Answer:nth-child 改為 nth-of-type
3.expected string or bytes-like object:
Answer:進行編碼轉換。
eg:re.findall(r’’,html.decode(‘utf-8’))
4. 'gbk' codec can't encode character '\xa5' in position 184823: illegal multibyte sequence
Answer:f = open(html,’w’,encoding = ‘utf-8’)
5. 網頁亂碼問題:中文亂碼
Answer :response = requests.get(url,headers = header).content
#content解決亂碼
6. expected string or bytes-like object
Answer:轉換為字串格式,str(urls)
urls = soup.select(' div > div.nei_left.fl > ul > li:nth-of-type(2)> a')[0]
Href = re.findall(r'<a href="(.*?)" target="" title=".*?">(.*?)</a>',str(urls))
7. Raise URLError (err) <urlopen error [Errno 11001] getaddrinfo failed>
通常,URLError在沒有網路連線(沒有路由到特定伺服器),或者伺服器不存在的情況下產生