python - BeautifulSoup:获取文本,创建字典

标签 python web-scraping beautifulsoup

我正在收集有关中央银行研究出版物的信息,到目前为止,对于美联储,我有以下 Python 代码:

START_URL = 'https://ideas.repec.org/s/fip/fedgfe.html'
page = requests.get(START_URL)
soup = BeautifulSoup(page.text, 'html.parser')
for paper in soup.findAll("li",class_="list-group-item downfree"):
    print(paper.text)

这为许多出版物中的第一个生成以下内容:

2018-070 Reliably Computing Nonlinear Dynamic Stochastic Model Solutions: An Algorithm with Error Formulasby Gary S. Anderson

我现在想把这个转换成一个Python字典,最终会包含大量论文:

Papers = {
  'Date': 2018 - 070,
  'Title': 'Reliably Computing Nonlinear Dynamic Stochastic Model Solutions: An Algorithm with Error Formulas',
  'Author/s': 'Gary S. Anderson'
  }

最佳答案

我在提取所有后代后得到了很好的结果,并且只挑选了那些 NavigableStrings 的后代。 .确保从 bs4 导入 NavigableString。我还使用了 numpy 列表理解,但您也可以使用 for 循环。

START_URL = 'https://ideas.repec.org/s/fip/fedgfe.html'
page = requests.get(START_URL)
soup = BeautifulSoup(page.text, 'html.parser')

papers = []
for paper in soup.findAll("li",class_="list-group-item downfree"):
    info = [desc.strip() for desc in paper.descendants if type(desc) == NavigableString]
    papers.append({'Date': info[0], 'Title': info[1], 'Author': info[3]})

print(papers[1])

{'Date': '2018-069',
 'Title': 'The Effect of Common Ownership on Profits : Evidence From the U.S. Banking Industry',
 'Author': 'Jacob P. Gramlich & Serafin J. Grundl'}

关于python - BeautifulSoup:获取文本,创建字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53187546/

相关文章:

python - 值错误 : unsupported format character while forming strings

python - 使用 BeautifulSoup 获取 span 之间的文本

javascript - ( Node )警告 : possible EventEmitter memory leak detected

python - 在 Beautifulsoup 的 find_all 中使用正则表达式

python - 首次使用后重新分配时局部变量上的 UnboundLocalError

python - 批量更新不工作pymongo

Python Web Scraper 问题

Python 传输机械化浏览器 session

python - 使用 beautifulsoup 提取 <br> 之间的文本,但没有下一个标签

Python 查找和替换 Beautiful Soup