|  | 
 
| 
import requests
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  from bs4 import BeautifulSoup
 if __name__ == '__main__':
 headers = {
 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
 }
 url = 'http://www.shicimingju.com/book/sanguoyanyi.html'
 page_text=requests.get(url=url,headers=headers).text.encode('iso-8859-1')
 page_soup=BeautifulSoup(page_text,'lxml')
 page_tage=page_soup.select('.book-mulu>ul>li')
 for li in page_tage:
 path=r'https://m.shicimingju.com/'+li.a['href']
 title=li.string
 detail_text=requests.get(url=path,headers=headers).text.encode('iso-8859-1')
 detail_soup=BeautifulSoup(detail_text,'lxml')
 content=detail_soup.find('div',class_='chapter_content')
 content=content.text
 print(title)
 print(content)
 
 为什么要用encode('iso-8859-1')才能不显示乱码啊?还有这个'iso-8859-1'是怎么知道该用他不用utf-8
 
 
https://www.cnblogs.com/huangchenggener/p/10983866.html | 
 |