【发布时间】: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