#--*--conding:utf-8 --*--
# Author: Gonggong
# 使用python爬取一个网页中表格的内容,并把抓取到的内容以json格式保存到文件中

import requests
from lxml import etree
import json


# 获取网页源代码
r = requests.get('http://ipwhois.cnnic.cn/bns/query/Query/ipwhoisQuery.do?queryOption=ipv4&txtquery=8.8.8.8')

# 使用xpath对爬取的源代码进行处理
dom_tree = etree.HTML(r.content)
links = dom_tree.xpath("/html/body/center[1]/table[1]/tr/td/font")

# 取出links的单行、双行的数据
res1 = [i.text for i in links[::2]]
res2 = [i.text for i in links[1::2]]

# 把两行数据组合成在一起
result = tuple(zip(res1, res2))

# 使用json格式保存到文件中
json.dump(result, open('/tmp/xpath_get.txt', 'w'), ensure_ascii=False)

 

相关文章:

  • 2021-04-18
  • 2021-11-19
  • 2021-11-11
  • 2022-01-18
  • 2021-11-08
  • 2021-11-25
  • 2021-11-19
猜你喜欢
  • 2022-12-23
  • 2022-02-24
  • 2022-12-23
  • 2021-05-05
  • 2021-12-17
  • 2021-05-16
  • 2022-12-23
相关资源
相似解决方案