我尝试了几种方法获取美联储新闻发布会的抄本(PDF格式)并将其转换为.txt文件,但失败了。下面是我的原始代码。如有任何建议,将不胜感激
import csv
from bs4 import BeautifulSoup
import requests
source=requests.get('https://www.federalreserve.gov/monetarypolicy/fomccalendars.htm').text
soup=BeautifulSoup(source,'lxml')
for b in soup.find_all("a",href=True):
if b.text=='Press Conference':
lnk='https://www.federalreserve.gov'+b['href']
source2=requests.get(lnk).text
soup2=BeautifulSoup(source2,'lxml')
for c in soup2.find_all("a",href=True):
if 'Press Conference Transcript'in c.text:
lnk2='https://www.federalreserve.gov'+c['href']
source3=requests.get(lnk2).text
soup3=BeautifulSoup(source3,'lxml')
for d in soup3.find_all("div",attrs={"id","content"}):
print(d)
fileout = open('conf.txt', 'a')
fileout.write(d)
因此,关于PDF抓取,我提出了以下建议:
此外,它可能值得一看How to extract text from a PDF file?
如果您坚持要签出库pyPDF2,请给出一个建议。如果您的PDF格式良好,则非常易于使用。代码示例看起来很简单,如下所示:
PDFMiner也是一个很好的例子
这篇来自RealPython博客的文章有点老,但也是一个很好的信息来源
相关问题 更多 >
编程相关推荐