scrapy 如何在Scarpy上删除IP的多余部分?

33qvvth1  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(183)

在Google上运行以下代码时:

response.ip_address

她的值等于:

IPv4Address('79.127.127.87')

截图:

但预期的答案如下:

'79.127.127.87'

如何得到这个答案?

ogsagwnx

ogsagwnx1#

只需使用str():

scrapy shell

In [1]: url='https://httpbin.org/'

In [2]: req = scrapy.Request(url=url)

In [3]: fetch(req)
[scrapy.core.engine] INFO: Spider opened
[scrapy.core.engine] DEBUG: Crawled (200) <GET https://httpbin.org/> (referer: None)

In [4]: str(response.ip_address)
Out[4]: '3.93.133.228'
vybvopom

vybvopom2#

由于你没有提到complete code,我建议这个方法...你试过后处理吗?

import re
ip = """(IPv4Address('79.127.127.87')"""

out = re.findall(r"'(.*?)'", ip, re.DOTALL)

# print(out)

# print(type(out))

ip_final = s = ''.join(out)
print(ip_final)

输出功率

79.127.127.87

相关问题