【问题标题】:Python program that crawls for external IP address爬取外部 IP 地址的 Python 程序
【发布时间】:2016-12-27 00:32:12
【问题描述】:

我创建了一个基本程序来尝试使用 BeautifulSoup 4 为我的外部 IP 地址抓取网站。虽然,我的程序不断收到属性错误,因为它无法获取 div 类的字符串或其他任何东西。它会显示为特定的 div 类不存在,因此无法对其进行爬网。我确实知道它存在,即使它说它不存在。有谁知道怎么回事?

这是我的代码:

import requests, sys, io
from html.parser import HTMLParser
from bs4 import BeautifulSoup

url = "https://www.iplocation.net/find-ip-address"
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, "cp437", "backslashreplace")
sourcecode = requests.get(url)
plaintext = sourcecode.text
soup = BeautifulSoup(plaintext, "html.parser")

tag = soup.find("span", {"style": "font-weight: bold; color:green;"})
print(tag)
ip = tag.string
print(ip)

【问题讨论】:

  • 该网站上没有一个 span 元素。
  • 当我检查网站的元素时,它说<span style="font-weight: bold; color:green;">00.00.000.00</span>
  • @Rawing 是<p style="font-size:1.4em;" align="center">Your IP Address is <span style="font-weight: bold; color:green;">00.00.000.00</span>.</p>的子元素
  • 你说得对,我又试了一次,但加载方式不同。不知道为什么网站随机更改。
  • 我在呈现的页面上看到它......它可能是由 javascript 生成的。在使用 beautifulsoup 之前,我会使用 selenium 而不是请求来加载页面。

标签: python python-3.x beautifulsoup ip web-crawler


【解决方案1】:

它与Javascript无关,如果你查看返回的源代码你可以看到:

<html style="height:100%"><head><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><meta name="format-detection" content="telephone=no"><meta name="viewport" content="initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"></head><body style="margin:0px;height:100%"><iframe src="/_Incapsula_Resource?CWUDNSAI=24&xinfo=9-52943897-0 0NNN RT(1471643127529 69) q(0 -1 -1 -1) r(0 -1) B12(8,881022,0) U10000&incident_id=198001480102412051-472966643371608393&edet=12&cinfo=08000000" frameborder=0 width="100%" height="100%" marginheight="0px" marginwidth="0px">Request unsuccessful. Incapsula incident ID: 198001480102412051-472966643371608393</iframe></body></html>

他们检测到你是一个机器人,并且没有给你你期望的来源。

您可以使用 wtfismyip.com 以 json 格式获取您的 ip 和更多信息:

url = "http://wtfismyip.com/json"
js = requests.get(url).json()
print(js)

或者只是你的 ip 使用 httpbin:

url = "http://httpbin.org/ip"
js = requests.get(url).json()
print(js)

【讨论】:

  • 附带说明,为什么我的外部 IP 地址在三个不同的网站上保持不变,但它们都给出了不同的位置?就像wtfismyip 说的是洛杉矶,然后iplocation 说的是阿肯色州的某个城镇。
  • 通过使用你的方法,它返回{'origin': '00.00.000.00'} 那么我怎么才能得到字符串00.00.000.00?
  • js["origin"],访问就像你访问任何字典一样
【解决方案2】:

如上所述,在服务器上放置了 bot 检测机制,如果您尝试执行 requests.get,那么它会返回“请求不成功。封装事件 ID:415000500153648966-193432437842182947”并且由于未加载源代码,因此您无法找到所需的信息。 如果你想用beautifulsoup来做,在selenium和beautifulsoup的帮助下你可以得到它,这里是示例代码:

如果没有安装 selenium,请先执行“pip install selenium”并从“https://sites.google.com/a/chromium.org/chromedriver/downloads”下载 chromedriver

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome("**Path to chrome driver**\chromedriver.exe")
driver.get('https://www.iplocation.net/find-ip-address')
content = driver.page_source.encode('utf-8').strip()
soup = BeautifulSoup(content,"html.parser")
tag = soup.find("span", {"style": "font-weight: bold; color:green;"}).text
print(tag)

它将打印:xxx.xx.xxx.xxx

注意:有时当您第一次在新机器上启动脚本时,它可能会要求验证码,手动输入它然后脚本就会运行

【讨论】:

    猜你喜欢
    • 2021-11-20
    • 2013-04-03
    • 2016-03-10
    • 2011-01-19
    • 2011-03-16
    • 2011-07-29
    • 2016-05-08
    • 2011-02-25
    相关资源
    最近更新 更多