# -*- coding:utf-8 -*-
import requests,time
from lxml import etree
from prettytable import PrettyTable
def getPage(url,headers):
r = requests.get(url,headers=headers)
if r.status_code != 200:
pass
r.encoding='utf-8'
html=etree.HTML(r.text)
result=html.xpath('//tbody/tr[position()>1]')
for i in result:
#因为有人的财富来源为空。会出错。所以定义了一个函数。
def getfour():
if len(i.xpath('td[4]/text()'))==0:
return 'null'
else:
return i.xpath('td[4]/text()')[0]
#写入到表格对象
fuhao.add_row([i.xpath('td[1]/text()')[0],i.xpath('td[2]//p/text()')[0],i.xpath('td[3]/text()')[0],getfour(),i.xpath('td[5]/a/text()')[0]])
url="https://www.phb123.com/renwu/fuhao/shishi.html"
base_url="https://www.phb123.com/renwu/fuhao/shishi_"
headers={
&
Python中爬虫结果使用表格美化显示
于 2022-05-09 19:07:50 首次发布