【问题标题】:How to take data from variable and put it into another如何从变量中获取数据并将其放入另一个变量中
【发布时间】:2015-07-15 00:48:40
【问题描述】:

我遇到了一点问题: 我想获取这些数据,

for item in g_data:
    print item.contents[1].find_all("a", {"class":"a-link-normal s-access-detail-page a-text-normal"})[0]["href"]
    print item.contents[1].find_all("a", {"class":"a-link-normal s-access-detail-page a-text-normal"})[1]["href"]
    print item.contents[1].find_all("a", {"class":"a-link-normal s-access-detail-page a-text-normal"})[2]["href"]
    print item.contents[1].find_all("a", {"class":"a-link-normal s-access-detail-page a-text-normal"})[3]["href"]

并在另一个进程中使用结果。

代码当前打印出亚马逊搜索词第一页的 url,我想获取这些 url,然后在页面上抓取数据。我将如何制作它,使其变成这样:

如果for item in g_data 返回url,则接受url[1:15] 并使用'x' 它。

如果for item in g_data 没有返回url,说"No urls to work with"

您能提供的任何帮助或线索都会非常棒,再次感谢。

【问题讨论】:

    标签: python web-scraping beautifulsoup screen-scraping


    【解决方案1】:

    如果您想获取g_data 中的每个项目,请查找项目中的所有 url,如果有,请对它们执行 x,如果项目中没有 url,则只需打印一些内容,那么这应该可以:

    def do_x(url):
        """ Does x with the given url. """
        short = url[1:15]
        # do x with short
        # ...
    
    # process all items in g_data
    for item in g_data:
        # find all links in the item
        links = item.contents[1].find_all("a", {"class":"a-link-normal s-access-detail-page a-text-normal"})
    
        if not links:
            # no links in this item -> skip
            print("No urls to work with.")
            continue
    
        # process all links
        for link in links:
            urls = link["href"]
            # process each url
            for url in urls:
                do_x(url)
    

    这是你想要的吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多