Python爬虫每次运行时都会访问一次主机,为了避免增加主机访问负荷,一般都是在本地解析。Python解析本地html文件方法如下:
1.将html文件本地保存
2.在Python中打开html文件,可使用BeautifulSoup方法直接打开
soup=BeautifulSoup(open('ss.html',encoding='utf-8'),features='html.parser')
3.获取本地文件资料
a.先爬取主页的列表资料,其中同义内容使用“@”符号连接,首先在for循环内给定一个值获取标签内的链接link=x.get('href'),接着使用sub方法指定删除link。代码如下:
link=x.get('href')
change2=re.sub(link,'',s)
def draw_base_list(doc):
lilist=soup.find('div',{'class':'babynames-term-articles'}).findAll('article');
#爬取一级参数
for x in lilist:
str1=''
count=0
a='@'
EnName=x.find('a').text;
Mean=x.find('div',{'class':'meaning'}).text;
Sou=x.find(