Python:url列表中的第二个url返回None

问题描述 投票:0回答:1

我正在使用漂亮的汤来搜索网址列表。

说我得到了一个名为卫星阵列的网址列表

from bs4 import BeautifulSoup as bs
import pandas as p
import numpy as np
import requests as r
import json

satellites = [
"https://solarsystem.nasa.gov/missions/insight/in-depth/",
"https://solarsystem.nasa.gov/missions/mars-cube-one/in-depth/",
"https://solarsystem.nasa.gov/missions/osiris-rex/in-depth/",
"https://solarsystem.nasa.gov/missions/exomars-trace-gas-orbiter/in-depth/",
"https://solarsystem.nasa.gov/missions/hayabusa-2/in-depth/"]

# Then my code starts with:

for sat in satellites:
    print(sat)
    page = r.get(sat)
    print("got page")
    soup = bs(page.content, "html.parser")
    print("got soup")

获取页面内容适用于第一个URL,但第二个返回None,这给我一个追溯'无'没有属性'内容'。

我切换了第一个和第二个网址,结果是一样的。所有的网址都经过了有效测试。

在网址上使用请求是否存在问题?

python url web-scraping beautifulsoup
1个回答
0
投票

因此,在查看Requests库文档之后,我发现了Session(),它通过整个脚本使一个Requests会话保持活动状态。

试过,它工作正常。

所以

导入请求为r

s = r.Session()

卫星= [...]

坐在卫星上:s.get(sat)

如果发生任何事情,上述内容不会返回None。似乎直接使用Requests.get('')在某些平台上存在问题。我目前是Mac OS High Sierra。如果有人说他们没有问题是在Linux上,这可能是平台依赖的,但我不能推测。

© www.soinside.com 2019 - 2024. All rights reserved.