python爬虫爬取网页表格数据
2018-03-07 10:41
771 查看
用python爬取网页表格数据,供大家参考,具体内容如下
from bs4 import BeautifulSoup import requests import csv import bs4 #检查url地址 def check_link(url): try: r = requests.get(url) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: print('无法链接服务器!!!') #爬取资源 def get_contents(ulist,rurl): soup = BeautifulSoup(rurl,'lxml') trs = soup.find_all('tr') for tr in trs: ui = [] for td in tr: ui.append(td.string) ulist.append(ui) #保存资源 def save_contents(urlist): with open("D:/2016年中国企业500强排行榜.csv",'w') as f: writer = csv.writer(f) writer.writerow(['2016年中国企业500强排行榜']) for i in range(len(urlist)): writer.writerow([urlist[i][1],urlist[i][3],urlist[i][5]]) def main(): urli = [] url = "http://www.maigoo.com/news/463071.html" rs = check_link(url) get_contents(urli,rs) save_contents(urli) main()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- python爬虫爬取网页表格数据
- 【Python爬虫5】提取JS动态网页数据
- Selenium学习三——利用Python爬取网页表格数据并存到excel
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- 采用python的pyquery引擎做网页爬虫,进行数据分析
- Python爬虫实战(4):豆瓣小组话题数据采集—动态网页
- [resource-]Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- 复杂的网页爬虫,python获取网页指的格式数据
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器库
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- Python爬虫入门学习--(向网页提交数据)
- python --网页爬虫,文本处理,科学计算,机器学习,数据挖掘资料+附带工具包下载+tensorflow
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- Python的网页爬虫&文本处理&科学计&机器学习&数据挖掘工具集
- python python 入门学习之网页数据爬虫搜狐汽车数据库