亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

我用的python3.7環境,html.parser解析器無法解析,也不報錯?怎么回事?

from?bs4?import?BeautifulSoup
import?re
from?urllib.parse?import?urlparse

class?HtmlParser(object):
????def?_get_new_urls(self,?url,?soup):
????????new_urls?=?set()
????????links?=?soup.find_all('a',href=re.compile(r"item/\d"))
????????for?link?in?links:
????????????new_url?=?link['href']
????????????new_full_url?=?urlparse.urljoin(url,new_url)
????????????new_urls.add(new_full_url)
????????return?new_urls

????def?_get_new_data(self,?url,?soup):
????????res_data?=?{}

????????res_data['url']?=?url

????????title_node?=?soup.find('dd',class_="lemmaWgt-lemmaTitle-title").find('h1')
????????res_data['title']?=?title_node.get_text()

????????summary_node?=?soup.find('dev',class_="lemma-summary")
????????res_data['summary']?=?summary_node.get_text()

????????return?res_data


????def?parse(self,?url,?html_cont):
????????if?url?is?None?or?html_cont?is?None:
????????????return
????????soup?=?BeautifulSoup(html_cont,'html.parser')
????????new_urls?=?self._get_new_urls(url,soup)
????????new_data?=?self._get_new_data(url,soup)
????????return?new_urls,new_data


正在回答

4 回答

links?=?soup.find_all('a',href=re.compile(r"item/\d"))

沒有匹配到這個詞條,建議修改再試

links?=?soup.find_all('a',href=re.compile(r"item/.{0,20}/\d+$"))


0 回復 有任何疑惑可以回復我~
python3對urllib和urllib2進行了重構,拆分成了urllib.request,?urllib.response,?urllib.parse,?urllib.error等幾個子模塊,這樣的架構從邏輯和結構上說更加合理。

urljoin現在對應的函數是urllib.parse.urljoin


0 回復 有任何疑惑可以回復我~
soup?=?BeautifulSoup(html_cont,'html.parser',from_encoding='utf-8')


0 回復 有任何疑惑可以回復我~
1.?summary_node?=?soup.find('dev',class_="lemma-summary")應該是:summary_node?=?soup.find('div',class_="lemma-summary")

2.soup?=?BeautifulSoup(html_cont,'html_parser')


1 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Python開發簡單爬蟲
  • 參與學習       227596    人
  • 解答問題       1288    個

本教程帶您解開python爬蟲這門神奇技術的面紗

進入課程

我用的python3.7環境,html.parser解析器無法解析,也不報錯?怎么回事?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號