【问题标题】:requests.exceptions.MissingSchema: Invalid URL 'h': No schema suppliedrequests.exceptions.MissingSchema:无效的 URL 'h':未提供架构
【发布时间】:2018-06-02 13:28:05
【问题描述】:

我正在做一个网页抓取项目,但遇到了以下错误。

requests.exceptions.MissingSchema:无效的 URL '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)

【问题讨论】:

  • 总是将完整的错误消息(Traceback)放在有问题的地方(作为文本,而不是屏幕截图)。还有其他有用的信息。例如它显示了哪条线有问题。
  • 你的错误是双重 for 循环 - 使用 print 来显示变量中的值,你会看到你犯了什么愚蠢的错误。
  • 据我了解没有任何错误,他们只是没有得到准确的信息吗?
  • @P.hunter 问题指示requests.exceptions.MissingSchema
  • 是的,我明白了,谢谢

标签: python web-scraping python-requests


【解决方案1】:

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

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

ref['href'] 为您提供单个 url,但您在下一个 for 循环中将其用作列表。

所以你有

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)

顺便说一句:如果你在(ref['href'], ) 中使用逗号,那么你会得到元组,然后第二个for 可以正常工作。


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

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

编辑:现在它不会多次读取相同的网址

编辑:现在它从第一个链接获取文本和 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)

【讨论】:

  • 现在我需要将所有的数据框输出放到一个 df 中
  • 您可以创建多个df 并使用merge, join, or concatenate 创建一个包含所有数据的df
  • 您还可以在开始时创建单个 df 并在循环中创建 append() 新行。
  • 您也可以在开始时创建单个列表和append()要列表的数据,最后将单个列表转换为单个df
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 2019-10-30
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多