【问题标题】:Make it so lists can go through the function使其列表可以通过该功能
【发布时间】:2020-01-06 21:33:56
【问题描述】:
for url in indexurls:
    row = parseFormPage(indexurls)
row

indexurls 是一个由 url 填充的列表

返回错误而不是遍历列表。 在迭代方面需要帮助! 这是功能代码

def parseFormPage(indexurls):
    '''
    Input: URL

    Output:
        filer_cik:
        filing_date:
        report_date:
        form_url
    '''

    # get page and create soup
    res = requests.get(indexurls)
    html = res.text
    soup = BeautifulSoup(html, 'html.parser')

    # parse filer Info on 10K page
    filer_div = soup.find('div', {'id': 'filerDiv'})
    filer_text = filer_div.find('span', {'class': 'companyName'}).find('a').get_text()
    filer_cik = re.search(r"(\d{10})\s(\(.+\))$" ,filer_text)[1]

    # parse 10K Page Meta data
    form_content = soup.find('div', {'class': 'formContent'})

    filing_date = form_content.find('div', text='Filing Date').findNext('div').get_text()
    report_date = form_content.find('div', text='Period of Report').findNext('div').get_text()

    # parse 10-K URL
    table = soup.find('table', {'class': 'tableFile', 'summary': 'Document Format Files'})
    href = table.find('td', text='10-K').find_parent('tr').find('a')['href']
    form_url = "https://www.sec.gov" + href

    return filer_cik, filing_date, report_date, form_url

错误如下

这是下面的错误,我认为它更多是列表的问题?

InvalidSchema: No connection adapters were found for '['https://www.sec.gov/Archives/edgar/data/1599407/0001477932-16-009759-index.htm']

这是一个示例:https://www.sec.gov/Archives/edgar/data/1599407/0001477932-16-009759-index.htm

预期的输出应该是一个列表:

('0001606163',
 '2016-03-21',
 '2015-12-31',
 'https://www.sec.gov/Archives/edgar/data/1606163/000114420416089184/v434424_10k.htm')

【问题讨论】:

  • 你能提供一个示例网址和预期的输出吗?
  • 错误是什么?
  • requests.get 采用一个 URL,但您传递给它的是一个列表。是这个问题吗?
  • 我想可能是这样,有没有办法让列表可以通过它?
  • @wjandrea 可能是对的;如果您的文件的 url 在列表中,只需创建一个循环:删除函数定义,然后执行 for url in indexurls: res = requests.get(url) 之类的操作或类似的操作。

标签: python function iteration


【解决方案1】:

requests.get 接受一个 URL,但您传递给它的是一个列表。是这个问题吗?

要修复它,您将更改循环:

for url in indexurls:
    row = parseFormPage(url)

加上重命名函数参数:

def parseFormPage(indexurl):
    ...
    res = requests.get(indexurl)
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多