Posted in Python onMarch 07, 2018
用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爬虫爬取网页表格数据
- Author -
zhuxunyuoyi声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@