【问题标题】:How to get the link I get redirected to如何获取我被重定向到的链接
【发布时间】:2018-10-19 03:15:01
【问题描述】:

我的程序中有一个带有配置文件的程序,您可以在其中添加公司股票代码,然后它会获取该配置文件中的股票代码并搜索新闻文章,这些文章都是从 API 中提取的所有信息。我打印出的类别之一是 URL 字段,因此我打印出的 URL 是将您重定向到另一个 URL(真实 URL)的 URL。现在我要做的是获取重定向的 URL 以打印出来

我有一个全局 companyurl 列表,我将添加我拉到的所有 URL,因此所有通用重定向 URL 都在其中。我得到了重定向的 URL,唯一的问题是我只得到了 1 个重定向的 URL,而它又为我正在打印的每篇新闻文章打印该 URL。这有点难以解释,所以如果您需要进一步说明,请询问。

这是我的代码,我注释掉的就是我正在尝试的。

出于测试目的,您可以将 2 个股票代码添加到配置文件中。如果您想测试一些东西:aapl 和 yelp,只需将它们放在配置文件中的不同行。

import sys
import json
import urllib.request
import time
import datetime
import requests

def main():
    openconfigfile()
    searchfornews()

def openconfigfile():
    mylist = []
    with open('config.txt') as myfile:
        for company in myfile:
            mylist.append(company.strip())
    return mylist

companyurl = []
def searchfornews():
    myurl = []
    global companyurl
    url = 'https://api.iextrading.com/1.0/stock/'
    companies = openconfigfile()
    for company in companies:
        stockinput = company + '/news/last/2'
        createdurl = url + stockinput
        myurl.append(createdurl)
    while True:
        try:
            for url in myurl:
                fob = urllib.request.urlopen(url)
                data = fob.read().decode('utf-8')
                companydata = json.loads(data)
                for company in companydata:
                    company['datetime'] = reformatdate()
                    companyurl.append(company['url'])
                    # r = getredirectedlink()
                    # company['url'] = r.url

                    print('''======== [%s] ========
%s:   "%s"
%s
tags: %s''' % (company['datetime'], company['source'], company['headline'], company['url'], company['related']))

            time.sleep(30)
        except Exception as e:
            print()
            print('''ERROR: news not found for 1 or more stock symbols
You have a stock symbol in the config file that doesnt match any known stock symbol''', e)
            time.sleep(30)

def reformatdate():
    time = datetime.datetime.today()
    newtime = time.strftime('%B %d %Y, %I:%M %p')
    return newtime

# def getredirectedlink():
#     global companyurl
#     for x in companyurl:
#         r = requests.get(x)
#         return r

if __name__ == '__main__':
    sys.exit(main())

【问题讨论】:

    标签: python python-3.x url-redirection


    【解决方案1】:

    你快完成了。你只需要改变两件事:

    1. searchfornews内:

      company['datetime'] = reformatdate()
      companyurl.append(company['url'])
      # r = getredirectedlink()
      # company['url'] = r.url
      

      改成

      company['datetime'] = reformatdate()
      company['url'] = getredirectedlink(company['url'])
      companyurl.append(company['url'])
      

      并将getredirectedlink 更改为以下内容:

      def getredirectedlink(companyurl):
          r = requests.get(companyurl)
          return r.url
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-25
      • 2014-06-29
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多