【问题标题】:Error crawling through locally hosted website爬取本地托管网站时出错
【发布时间】:2018-10-07 11:21:27
【问题描述】:

我想爬取当前托管在本地的网站。不能爬取本地托管的网站吗?我收到此错误:

 File "C:/Users/hero/PycharmProjects/project/Crawler.py", line 22, in <module>
    imagefile.write(urllib.request.urlopen("http://192.168.1.1/Webpage.html"+img_src).read())
urllib.error.HTTPError: HTTP Error 404: Not Found

爬虫代码:

import urllib.request
from bs4 import BeautifulSoup


def make_soup(url):
    thepage = urllib.request.urlopen(url)
    soupdata = BeautifulSoup(thepage, "html.parser")
    return soupdata


i = 1
soup = make_soup("http://192.168.1.1/Webpage.html")

unique_srcs = []
for img in soup.findAll('img'):
    if img.get('src') not in unique_srcs:
        unique_srcs.append(img.get('src'))
for img_src in unique_srcs:
    filename = str(i)
    i = i + 1
    imagefile = open(filename + '.png', 'wb')
    imagefile.write(urllib.request.urlopen("http://192.168.1.1/Webpage.html"+img_src).read())
    imagefile.close()

【问题讨论】:

  • 你好。这听起来像是一个愚蠢的问题,但您是否尝试过使用浏览器访问192.168.1.1/Webpage.html?你的网络呢? 192.168.1.1 通常是路由器的 IP 地址,如果它是您的 Web 服务器的 IP,我会感到惊讶。
  • 192.168.1.1 这是我为发布问题而保留的 ip。但是,当我保留自己的 ip 时,它正在工作。但是,爬取有错误

标签: python beautifulsoup web-crawler


【解决方案1】:

您忘记在 url 路径中添加斜杠 /

只需将行更改为如下所示:

imagefile.write(urllib.request.urlopen("http://192.168.1.1/Webpage.html/"+img_src).read())

【讨论】:

  • 我仍然遇到同样的错误:urllib.error.HTTPError: HTTP Error 404: Not Found
  • 在运行这条语句之前变量img_src的值是多少?
  • 只是运行for循环。 unique_src 由那些图像尚未抓取的 src 组成
  • 是的,我知道。只需在imagefile.write(urllib.request.urlopen("http://192.168.1.1/Webpage.html/"+img_src).read()) 之前添加print(img_src)。只是为了检查列表中是否不存在任何不需要的值。
猜你喜欢
  • 2019-07-20
  • 2015-03-24
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
相关资源
最近更新 更多