1062195630 发表于 2021-7-11 14:54:30

爬虫求助

import requests
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

青出于蓝 发表于 2021-7-11 15:19:55

https://www.cnblogs.com/huangchenggener/p/10983866.html
页: [1]
查看完整版本: 爬虫求助