问题描述
当我在本地运行该脚本时,该脚本运行良好,但是当我在Heroku上上传该脚本时, requests.get()函数无法打开返回
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()可能有问题,更改格式有时会有所帮助。此外,对相同网站的请求过多后,我也遇到了同样的问题。我想连接会阻止您这样做。您可以重新启动路由器。那对我有用。
如果使用以下一种方法可以解决您的问题,请写出