【问题标题】:How can I assign web scraping outputs to an array using python?如何使用 python 将网络抓取输出分配给数组?
【发布时间】:2016-11-04 01:27:04
【问题描述】:

我想执行此操作并从 title 和 href 属性中获取所有文本。代码运行,我确实得到了所有需要的数据,但我想将输出分配给一个数组,当我尝试分配它时,它只会给我最后一个属性在 HTML 中为真的实例。

from bs4 import BeautifulSoup
import urllib

r = urllib.urlopen('http://www.genome.jp/kegg-bin/show_pathway?map=hsa05215&show_description=show').read()
soup = BeautifulSoup((r), "lxml")
for area in soup.find_all('area', href=True):
    print area['href']
for area in soup.find_all('area', title=True):
    print area['title']

如果有帮助,我会这样做,因为稍后我将创建一个包含数据的列表。我刚刚开始学习,所以非常感谢额外的解释。

【问题讨论】:

    标签: python-2.7 web-scraping beautifulsoup urllib2


    【解决方案1】:

    你需要使用 list comprehensions:

    links = [area['href'] for area in soup.find_all('area', href=True)]
    titles = [area['title'] for area in soup.find_all('area', title=True)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      相关资源
      最近更新 更多