【问题标题】:Generate a list to feed of urls into web scraper生成一个列表以将 url 馈送到网络爬虫中
【发布时间】:2017-02-19 13:06:44
【问题描述】:

几个月来,我一直在学习 Python 作为第一语言,并且正在尝试构建一个网络爬虫,而不是依赖于我提供给它的 url,而是爬取一个网站来为我获取 url。

我已确定网站的哪些部分包含我需要的 url,并且知道/认为我需要 2 个列表来做我想做的事。

第一个是城市的 url 列表,第二个是这些城市中单位的 url 列表。这是我最终想要迭代并从中刮取数据的单元的网址。到目前为止,我有以下代码:

def get_cities():
    city_sauce = urllib.request.urlopen('the_url')
    city_soup = BeautifulSoup(city_sauce, 'html.parser')
    the_city_links = []
    for city in city_soup.findAll('div', class_="city-location-menu"):
        for a in city.findAll('a', href=True, text=True):
                the_city_links.append('first_half_of_url' + a['href'])
    return the_city_links

当我打印出来时,它会显示我需要的所有 url,所以我认为我已经成功地在这里创建了一个链接列表?

第二部分如下:

def get_units():
    for theLinks in get_cities():
        unit_sauce = urllib.request.urlopen(theLinks)
        unit_soup = BeautifulSoup(unit_sauce, 'html.parser')
        the_unit_links = []
        for unit in unit_soup.findAll('div', class_="btn white-green icon-right-open-big"):
            for aa in unit.findAll('a', href=True, text=True):
                the_unit_links.append(aa)
        return the_unit_links

打印时,它只返回 []。我不确定我哪里出错了,任何帮助将不胜感激!

第 2 部分修订:

def get_units():
    for the_city_links in get_cities():
        unit_sauce = urllib.request.urlopen(the_city_links)
        unit_soup = BeautifulSoup(unit_sauce, 'html.parser')
        the_unit_links = []
        for unit in unit_soup.findAll('div', class_="btn white-green icon-right-open-big"):
            for aa in unit.findAll('a', href=True, text=True):
                the_unit_links.append(aa)
        return the_unit_links

【问题讨论】:

  • 您需要提供您要获取的链接?可能是您缺少获取某些内容,或者您​​获取的类有误。
  • 我把网址放在city_sauce 中,我希望unit_sauce 会获取这些链接中的每一个,这些链接存储在一个列表中,在unit_soup 中解析它们,然后进入每个的链接并在'div', class_="btn white-green icon-right-open-big" 处获取href,然后将它们添加到the_unit_links 列表中,然后在我的刮板中对其进行迭代。有任何想法吗? @PiyushS.Wanare 我对第二部分稍作修改,请参阅修订。
  • 如果你把数据放在一个函数本身会更好。

标签: python list python-3.x loops web-scraping


【解决方案1】:

假设我了解您的使用方式 - 您的函数将在 get_cities() 中的第一个链接之后返回,这可能没有单位?我认为您需要在函数的开头设置 the_unit_links = [],然后将函数的返回行移入一个缩进 - 所以它只有在 get_cities 中的所有链接都被抓取后才会返回。

【讨论】:

  • 感谢您的建议,不幸的是,它也返回了 []!
【解决方案2】:
def getLinks():
    city_sauce = urllib.request.urlopen('the_url')
    city_soup = BeautifulSoup(city_sauce, 'html.parser')
    the_city_links = []

    for city in city_soup.findAll('div', class_="city-location-menu"):
            for a in city.findAll('a', href=True, text=True):
                    the_city_links.append('first_half_of_url' + a['href'])
        #return the_city_links

    # print the_city_links

    for the_city_links in the_city_links:
        unit_sauce = urllib.request.urlopen(the_city_links)
        unit_soup = BeautifulSoup(unit_sauce, 'html.parser')
        the_unit_links = []
        for unit in unit_soup.findAll('div', class_="btn white-green icon-right-open-big"):
            for aa in unit.findAll('a', href=True, text=True):
                the_unit_links.append(aa)
        return the_unit_links

注意:- Print the_city_links 并检查您是否获得了预期的输出,然后在其上运行另一个循环以获取其对应的 unit_links

【讨论】:

    【解决方案3】:
    # Crawls main site to get a list of city URLs
    def getCityLinks():
        city_sauce = urllib.request.urlopen('the_url')
        city_soup = BeautifulSoup(city_sauce, 'html.parser')
        the_city_links = []
    
        for city in city_soup.findAll('div', class_="city-location-menu"):
            for a in city.findAll('a', href=True, text=True):
                the_city_links.append('the_url' + a['href'])
        #print(the_city_links)
        return the_city_links
    
    # Crawls each of the city web pages to get a list of unit URLs
    def getUnitLinks():
        getCityLinks()
        for the_city_links in getCityLinks():
            unit_sauce = urllib.request.urlopen(the_city_links)
            unit_soup = BeautifulSoup(unit_sauce, 'html.parser')
            the_unit_links = []
            for unit_href in unit_soup.findAll('a', class_="btn white-green icon-right-open-big", href=True):
                the_unit_links.append('the_url' + unit_href['href'])
            yield the_unit_links
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多