php抓取天眼查,python用xpath采集天眼查内容,有反爬,zip拼数据

# -*- coding: utf-8 -*-

"""

起始未登录有反爬,爬失败。策略:重新登录,并在headers中加入cookie,成功了。

"""

import requests

from lxml import etree

headers={

'Cookie':'your long cookie',

#请求类型

'Origin':'https://www.tianyancha.com',

'User-Agent':'Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/82.0.4068.4 Safari/537.36'

}

url='https://www.tianyancha.com/search?key=%E4%BA%AC%E4%B8%9C';

#查看请求headers,response=requests.get(url).request.headers是个爬虫

response=requests.get(url,headers=headers).text

html=etree.HTML(response)

#//从根节点,//div表示全选所有div

name=html.xpath('//div[@class="header"]/a/text()')

#print(name)

nickName=html.xpath('//div[@class="title -wider text-ellipsis"]/a/text()')

#print(nickName)

money=html.xpath('//div[@class="title -narrow text-ellipsis"]/span/text()')

#print(money)

#拼在一起

for n,N,m in zip(name,nickName,money):

#print(n,N,m),文件写入方式,文件存在就写入,不存在就创建。要用a

r=open('天眼查数据.txt','a')

r.write('{},{},{}\n'.format(n,N,m))

r.close()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值