Python Requests.Get-提供无效的架构错误

马特·威尔逊

另一个给你。

尝试从CSV文件中抓取网址列表。这是我的代码:

from bs4 import BeautifulSoup
import requests
import csv

with open('TeamRankingsURLs.csv', newline='') as f_urls, open('TeamRankingsOutput.csv', 'w', newline='') as f_output:
    csv_urls = csv.reader(f_urls)
    csv_output = csv.writer(f_output)


    for line in csv_urls:
        page = requests.get(line[0]).text
        soup = BeautifulSoup(page, 'html.parser')
        results = soup.findAll('div', {'class' :'LineScoreCard__lineScoreColumnElement--1byQk'})

        for r in range(len(results)):
            csv_output.writerow([results[r].text])

...这给了我以下错误:

Traceback (most recent call last):
  File "TeamRankingsScraper.py", line 11, in <module>
    page = requests.get(line[0]).text
  File "C:\Users\windowshopr\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\windowshopr\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\windowshopr\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\windowshopr\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 616, in send
    adapter = self.get_adapter(url=request.url)
  File "C:\Users\windowshopr\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 707, in get_adapter
    raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for 'https://www.teamrankings.com/mlb/stat/runs-per-game?date=2018-04-15'

我的CSV文件只是A列中几个网址的列表(即https:// www ..。

(我要抓取的div类在该页面上不存在,但这不是问题所在。至少我不认为。我可以更新它,以便可以从CSV中读取它。文件。)

有什么建议么?因为此代码可在另一个项目上使用,但是由于某些原因,我在此新的URL列表中遇到了问题。非常感谢!

用户名

从追溯, requests.exceptions.InvalidSchema: No connection adapters were found for 'https://www.teamrankings.com/mlb/stat/runs-per-game?date=2018-04-15'

请参阅网址中的随机字符,该字符应从 https://www.teamrankings.com/mlb/stat/runs-per-game?date=2018-04-15

因此,首先使用regex解析csv并删除http / https之前的所有随机字符。那应该解决您的问题。

如果您想在读取csv时使用此特定的网址解决当前的问题,请执行以下操作:

import regex as re

strin = "https://www.teamrankings.com/mlb/stat/runs-per-game?date=2018-04-15"

re.sub(r'.*http', 'http', strin)

这将为您提供请求可以处理的正确网址。

由于您要求对循环中可访问的路径进行全面修复,因此您可以执行以下操作:

from bs4 import BeautifulSoup
import requests
import csv
import regex as re

with open('TeamRankingsURLs.csv', newline='') as f_urls, open('TeamRankingsOutput.csv', 'w', newline='') as f_output:
    csv_urls = csv.reader(f_urls)
    csv_output = csv.writer(f_output)


    for line in csv_urls:
        page = re.sub(r'.*http', 'http', line[0])
        page = requests.get(page).text
        soup = BeautifulSoup(page, 'html.parser')
        results = soup.findAll('div', {'class' :'LineScoreCard__lineScoreColumnElement--1byQk'})

        for r in range(len(results)):
            csv_output.writerow([results[r].text])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在请求到达之前,requests.get()(Python,Requests模块)是否会暂停脚本?

Python:requests.get,循环访问URL

使用``Requests''python模块进行POST请求,接收响应就像是GET

python中的request.get提供连接超时错误

带有身份验证的requests.post为python中的jira rest api提供错误415

requests.exceptions.MissingSchema:无效的网址“ h”:未提供模式

Python3 get.requests url缺少参数

AWS Lambda上的Python:不赞成使用botocore.vendored的`requests`,但不提供`requests`

Python Requests_html:给我超时错误

与python合并requests.get

Requests.get未完成,不会引发任何错误

Requests.get Twitter API错误身份验证

python27 requests.get() 递归

Python ssl/https requests.get 调用失败

为什么来自 python 中 ngrok 的 requests.get 的原始内容不可读?

使用 python-requests 发送 get 请求

无法使用 sudo apt-get install 安装或找到 python-pip 和 python-requests

Python requests put request to shopify api 总是给出错误

使用 requests.get() 在 python 中对 XHR 请求的响应不完整

requests.get 很慢

Python 将文件中的列表传递给 requests.get()

在 Python 中使用 get 和 requests 时如何防止下载空的 pdf 文件?

模拟 requests.Session.get

requests.get 对图像很慢

dynamo get ValidationException 中的无服务器错误:提供的关键元素与架构不匹配

Patebin api在python中为requests.post返回“错误的API请求,使用POST请求,而不是GET”?

requests.get 返回错误 HTTPSConnectionPool Python

requests.get 上的问题(使用 python 3.8),甚至设置 verify=False

使用帶點的 URL 時,Python requests.get(URL) 返回 404 錯誤