我是靠谱客的博主 背后毛巾,这篇文章主要介绍python使用bs4库爬取网站数据存储到excel,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from urllib.request import urlopen from bs4 import BeautifulSoup import pandas as pd import re import pprint class Crawler(object): def __init__(self, url): self.url = url def parser_page(self): html = urlopen(self.url) soup = BeautifulSoup(html, "html.parser") tables = soup.find_all('div', class_='col middle-column-home') print(tables) for table in tables: # 此时的table是这样的<h4>【学习 HTML】</h4> # 方法一用正则去掉标签<h4> # name_regx = re.compile(r">(.*)<") # name = name_regx.findall(str(table))[0] # 方法二用bs4自带的直接取name.text name_infos = table.find_all('h4') name_list = [i.text for i in name_infos] describe_infos = table.find_all('strong') info_list = [i.text for i in describe_infos] table_dict = dict(zip(name_list, info_list)) # pprint.pprint(table_dict) return table_dict def write_excel(self): data = self.parser_page() with pd.ExcelWriter("test_data.xlsx") as wt: df = pd.DataFrame(data, index=['1']) df.to_excel(wt, sheet_name="lable", index=False) if __name__ == '__main__': cs = Crawler("url") cs.parser_page()

最后

以上就是背后毛巾最近收集整理的关于python使用bs4库爬取网站数据存储到excel的全部内容,更多相关python使用bs4库爬取网站数据存储到excel内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部