本次内容易错点
1.乱码的问题,要熟练使用以下自动识别html里的编码格式
html.encoding=html.apparent_encoding2.for循环的嵌套问题,要注意第2个for循环print的位置
        for article in articles:
            res+=article.text
        print('新闻内容:',res)最后放出全部代码,算是基础知识的一次复习
from bs4 import BeautifulSoup
import requests
url="https://finance.sina.com.cn/"
html=requests.get(url)
html.encoding=html.apparent_encoding
soup=BeautifulSoup(html.text,'lxml')
lis=soup.select('.m-p1-m-blk2 .m-p1-mb2-list.m-list-container ul li a')
for li in lis:
    title=li.text
    InnerUrl=li['href']
    if InnerUrl.endswith('shtml') and len(title)>3:
        print(title,InnerUrl)
        html=requests.get(InnerUrl)
        html.encoding=html.apparent_encoding
        soup=BeautifulSoup(html.text,'lxml')
        articles=soup.select('.article p')
        res=''
        for article in articles:
            res+=article.text
        print('新闻内容:',res)
        with open('xinlang.txt','a',encoding='utf8')as f:
            f.write(res+'\n')
 捡肥皂网
																		捡肥皂网
											 
            