requests.exceptions.MissingSchema:无效的 URL“h”:未提供架构

2024-04-07

我正在开发一个网络抓取项目,并遇到了以下错误。

requests.exceptions.MissingSchema:无效的 URL“h”:未提供架构。也许你的意思是http://h http://h?

下面是我的代码。我从 html 表中检索所有链接,它们按预期打印出来。但是当我尝试使用 request.get 循环它们(链接)时,我收到上面的错误。

from bs4 import BeautifulSoup
import requests
import unicodedata
from pandas import DataFrame

page = requests.get("http://properties.kimcorealty.com/property/output/find/search4/view:list/")
soup = BeautifulSoup(page.content, 'html.parser')

table = soup.find('table')
for ref in table.find_all('a', href=True):
    links = (ref['href'])
    print (links)
    for link in links:
        page = requests.get(link)
        soup = BeautifulSoup(page.content, 'html.parser')
        table = []
        # Find all the divs we need in one go.
        divs = soup.find_all('div', {'id':['units_box_1', 'units_box_2', 'units_box_3']})
        for div in divs:
            # find all the enclosing a tags.
            anchors = div.find_all('a')
            for anchor in anchors:
                # Now we have groups of 3 list items (li) tags
                lis = anchor.find_all('li')
                # we clean up the text from the group of 3 li tags and add them as a list to our table list.
                table.append([unicodedata.normalize("NFKD",lis[0].text).strip(), lis[1].text, lis[2].text.strip()])
        # We have all the data so we add it to a DataFrame.
        headers = ['Number', 'Tenant', 'Square Footage']
        df = DataFrame(table, columns=headers)
        print (df)

你的错误是第二个for代码中循环

for ref in table.find_all('a', href=True):
    links = (ref['href'])
    print (links)
    for link in links:

ref['href']为您提供单个网址,但您将其用作下一个列表for loop.

所以你有了

for link in ref['href']:

它给你 url 中的第一个字符http://properties.kimcore...这是h

完整的工作代码

from bs4 import BeautifulSoup
import requests
import unicodedata
from pandas import DataFrame

page = requests.get("http://properties.kimcorealty.com/property/output/find/search4/view:list/")
soup = BeautifulSoup(page.content, 'html.parser')

table = soup.find('table')
for ref in table.find_all('a', href=True):
    link = ref['href']
    print(link)
    page = requests.get(link)
    soup = BeautifulSoup(page.content, 'html.parser')
    table = []
    # Find all the divs we need in one go.
    divs = soup.find_all('div', {'id':['units_box_1', 'units_box_2', 'units_box_3']})
    for div in divs:
        # find all the enclosing a tags.
        anchors = div.find_all('a')
        for anchor in anchors:
            # Now we have groups of 3 list items (li) tags
            lis = anchor.find_all('li')
            # we clean up the text from the group of 3 li tags and add them as a list to our table list.
            table.append([unicodedata.normalize("NFKD",lis[0].text).strip(), lis[1].text, lis[2].text.strip()])
    # We have all the data so we add it to a DataFrame.
    headers = ['Number', 'Tenant', 'Square Footage']
    df = DataFrame(table, columns=headers)
    print (df)

BTW:如果你在中使用逗号(ref['href'], )然后你得到元组,然后是第二个for工作正常。


EDIT:它创建列表table_data在开始时并将所有数据添加到此列表中。最后转换成DataFrame。

但现在我看到它多次读取同一页面 - 因为在每一行中,每一列中都有相同的 url。您必须仅从一列获取 url。

EDIT:现在它不会多次读取同一个网址

EDIT:现在它从第一个链接获取文本和 hre 并在您使用时添加到列表中的每个元素append().

from bs4 import BeautifulSoup
import requests
import unicodedata
from pandas import DataFrame

page = requests.get("http://properties.kimcorealty.com/property/output/find/search4/view:list/")
soup = BeautifulSoup(page.content, 'html.parser')

table_data = []

# all rows in table except first ([1:]) - headers
rows = soup.select('table tr')[1:]
for row in rows: 

    # link in first column (td[0]
    #link = row.select('td')[0].find('a')
    link = row.find('a')

    link_href = link['href']
    link_text = link.text

    print('text:', link_text)
    print('href:', link_href)

    page = requests.get(link_href)
    soup = BeautifulSoup(page.content, 'html.parser')

    divs = soup.find_all('div', {'id':['units_box_1', 'units_box_2', 'units_box_3']})
    for div in divs:
        anchors = div.find_all('a')
        for anchor in anchors:
            lis = anchor.find_all('li')
            item1 = unicodedata.normalize("NFKD", lis[0].text).strip()
            item2 = lis[1].text
            item3 = lis[2].text.strip()
            table_data.append([item1, item2, item3, link_text, link_href])

    print('table_data size:', len(table_data))            

headers = ['Number', 'Tenant', 'Square Footage', 'Link Text', 'Link Href']
df = DataFrame(table_data, columns=headers)
print(df)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

requests.exceptions.MissingSchema:无效的 URL“h”:未提供架构 的相关文章

随机推荐

  • 通过 SSH 连接 MySQL 时遇到问题

    我正在本地 OS X 计算机上运行 Node Express 网站 我需要 ssh 到远程 mysql 数据库 以便我可以开始针对它编写查询 现在 当我通过 OS X Yosemite 终端执行此操作时 我可以 ssh 到云中的远程服务器
  • Firebase OrderByChild() 和 EqualTo() 无法正常工作

    我需要在随机键中找到现有的子项 并且我使用 OrderByChild 和 EqualTo 来过滤查询 但它的行为非常奇怪 有时它显示子项仅存在一个子项 有时它不起作用 我需要检查 February 2019 的子项 date expense
  • 从 Resources 子文件夹中获取文件名

    在我的资源文件夹中 我有一个图像子文件夹 我想从该文件夹中获取这些图像的所有文件名 尝试了几个Resources loadAll之后获取 name 但没有成功的方法 这是实现我在这里想做的事情的正确做法吗 没有内置 API 可以执行此操作
  • 从具有多个结果集的存储过程中检索数据

    给定 SQL Server 中的一个存储过程 它有多个select语句 有没有办法在调用过程时单独处理这些结果 例如 alter procedure dbo GetSomething as begin select from dbo Per
  • ASP.NET:权限/身份验证架构

    我正在考虑建立一个验证在我的 ASP NET 应用程序中 具有以下要求 一名用户只有一个角色 即管理员 销售经理 销售 角色拥有一组 CRUD 访问现有对象子集的权限 IE 销售人员对对象类型 产品 具有 CREAD READ WRITE
  • Attention机制中的“源隐藏状态”指的是什么?

    注意力权重计算如下 我想知道什么h s指 在tensorflow代码中 编码器RNN返回一个元组 encoder outputs encoder state tf nn dynamic rnn 正如我所想 h s应该是encoder sta
  • MessagePack:快速跨平台序列化器和RPC - 请分享经验

    寻找一些我偶然发现的快速 简单且稳定的 RPC 库消息包 http msgpack org 项目看起来非常好 它也正在积极开发中 如果您以任何方式使用它 可以分享一下您的经验吗 附 我认为这个问题应该是社区维基 好吧 过了一段时间 我发现
  • 访问 Django 模板中 ImageField 上的图像尺寸?

    I have ImageField在我的模型中 我可以在模板中显示图像 但是 如何检索图像的高度和宽度 请参阅文档图像场 https docs djangoproject com en 1 11 ref models fields djan
  • `sorted(list)` 与 `list.sort()` 有什么区别?

    list sort 对列表进行排序并替换原始列表 而sorted list 返回列表的排序副本 而不更改原始列表 什么时候一个人比另一个人更受青睐 哪个更有效率 多少 列表可以恢复到未排序状态吗list sort 已执行 Please us
  • web.config 中带点的路径

    我需要在 web config 文件中添加一个位置元素 但路径以点开头 而且我认为我无法更改该路径 它是为了让我们加密 http letsencrypt org自动化 如果我让点 就像
  • 将自定义 SecurityExpressionOperations 中的方法注册为 Spring SpEL 函数

    我有以下实现MethodSecurityExpressionOperations public class CustomMethodSecurityExpressionRoot extends SecurityExpressionRoot
  • Google 地图未显示在 Phonegap Build 中

    我的电话间隙期只有两个月左右 我一直在谷歌波纹模拟器中测试该应用程序 并且谷歌地图的一切都运行良好 但是 当我将此项目上传到phonegap build 并将其安装到我的Android 设备中时 谷歌地图不会显示 这是我的index htm
  • JQuery,从字符串中删除元素

    我有一个字符串 var s h1 heading h1 p para p 我想删除h1从中提取元素 我试过了 s remove h1 但 s 仍然包含h1元素 我也尝试过 s s remove h1 and h1 s remove and
  • 汇总每日内容

    我一直在尝试汇总 有些不稳定的 每日数据 我实际上正在处理 csv 数据 但如果我重新创建它 它看起来像这样 library zoo dates lt c 20100505 20100505 20100506 20100507 val1 l
  • Flutter 应用程序在直接调用 Firebase 云函数时出现 UNAUTHENTICATED 错误

    我尝试直接从我的 FLUTTER 应用程序使用 firebase 云函数 oncall 方法 即使我登录了 它仍然给我一个未经身份验证的错误 颤振应用程序代码 CloudFunctions function CloudFunctions i
  • MySQL - 删除级联上的外键 - 是否有定义的执行顺序?

    我的 MySQL 有问题 多个表上的 CASCADE ON DELETE 规则 显然 CASCADE ON DELETE 规则的执行顺序取决于它们的定义顺序 但是这个执行顺序是否明确定义或者取决于 MySQL 版本 这是我的三个表 A B
  • Sails.js:如何知道请求是否包含要上传的文件?

    我的一个控制器中有下一个操作 它的功能是上传图像文件或使用视频网址创建新的封面模型 它工作正常 但是当请求有标头时内容类型 多部分 表单数据并且不包含该操作引发超时错误的文件req file upload function req res
  • string.matches(regex) 返回 false,尽管我认为它应该是 true

    我正在使用 Java 正则表达式 哦 我真的很想念 Perl Java正则表达式太难了 无论如何 下面是我的代码 oneLine kind list items System out println oneLine matches kind
  • Ruby on Rails,没有交互式请求的模板

    在此输入图像描述 https i stack imgur com roT9c png 我之前运行过 rails generate controller Welcome index 在终端上运行命令并直接运行服务器 但不断出现此错误消息 没有
  • requests.exceptions.MissingSchema:无效的 URL“h”:未提供架构

    我正在开发一个网络抓取项目 并遇到了以下错误 requests exceptions MissingSchema 无效的 URL h 未提供架构 也许你的意思是http h http h 下面是我的代码 我从 html 表中检索所有链接 它