python - BeautifulSoup指定lxml作為解析器報錯?
問題描述
環境:windows 10PyCharm 2016.3.2
遇到問題:
剛開始學python,想用BeautifulSoup解析網頁,但出現報錯:
UserWarning: No parser was explicitly specified, so I’m using the best available HTML parser for this system ('lxml'). This usually isn’t a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.The code that caused this warning is on line 4 of the file C:/Users/excalibur/PycharmProjects/learn/getMyIP.py. To get rid of this warning, change code that looks like this: BeautifulSoup([your markup])to this: BeautifulSoup([your markup], 'lxml') markup_type=markup_type))
然后根據提示和官網的文檔加上:BeautifulSoup(markup, 'html.parser')
結果出現了這樣的報錯:
在Google搜了下,都是說要導入路徑,但是在 Settings -> Project -> Project Interpreter 里是這樣的
顯示BeautifulSoup已經導入了
請問我要怎么做才能解決這個問題呢?
萬分感謝!
問題解答
回答1:找了其他人的代碼看,終于知道是什么問題
并不是路徑的問題,而是傳參的問題
markup 其實是要解析的內容,例如:
soup = BeautifulSoup('<html>data</html>', 'lxml')
或者
markup = '<html>data</html>'soup = BeautifulSoup(markup, 'lxml')
PS. 在文檔中沒有函數參數列表之類的,不知道是不是找的位置錯了...
回答2:pip install lxml
相關文章:
1. python - Win7調用flup報錯’module’ object has no attribute ’fromfd’2. css3 - 微信前端頁面遇到的transition過渡動畫的bug3. javascript - 請教如何獲取百度貼吧新增的兩個加密參數4. 網頁爬蟲 - Python 爬蟲中如何處理驗證碼?5. Python如何播放還存在StringIO中的MP3?6. Python爬蟲如何爬取span和span中間的內容并分別存入字典里?7. css - input間的間距和文字上下居中8. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處9. mysql 一個sql 返回多個總數10. (python)關于如何做到按win+R再輸入文件文件名就可以運行?
