Python库请求返回“错误503”,在heroku上打开链接

问题描述

当我在本地运行该脚本时,该脚本运行良好,但是当我在Heroku上上传该脚本时, requests.get()函数无法打开返回 的Amazon链接。 em>,脚本以错误“ AttributeError:'NoneType'对象没有属性'get_text'”结尾(我想是因为 requests.get()无法打开Amazon链接)。如何解决该问题,以便 requests.get()在Heroku上返回

import requests
from bs4 import BeautifulSoup
import time

url = "here is written the Amazon product link"

while True:
    req = requests.get(url,headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'})
    soup = BeautifulSoup(req.content,"lxml")
    name = soup.find(id = "productTitle",class_ = "a-size-large product-title-word-break")
    price = soup.find(id = "priceblock_ourprice",class_ = "a-size-medium a-color-price priceBlockBuyingPriceString")
    name = name.get_text()
    price = price.get_text()
    print "New product: ",name,price
    time.sleep(10)

我的需求文件:

bs4==0.0.1
requests==2.22.0
lxml==4.5.2

我的Procfile文件:

web: python "test.py"

我尝试了 requests.get()的其他链接,但该方法有效,所以我认为亚马逊存在一些问题。

解决方法

我正在尝试编写将Google搜索结果作为链接的代码

searchinput = "dogs"
print('Searching...')
    
google_search = requests.get('https://www.google.com/search?q='+searchinput)
soup = bs4.BeautifulSoup(google_search.text,'html.parser')

search_results = soup.select('div#main > div > div > div > a')
#for each in search_results:
   #print (each)
   #print("-----------------------")
#print (len(search_results))
linksList = []
......

,它继续进行字符串操作。我也有同样的问题。首先,您可以将我的代码用于请求和beatifulSoup。也许您对我的request.get()可能有问题,更改格式有时会有所帮助。此外,对相同网站的请求过多后,我也遇到了同样的问题。我想连接会阻止您这样做。您可以重新启动路由器。那对我有用。

如果使用以下一种方法可以解决您的问题,请写出

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...