【问题标题】:Having Trouble scraping有问题刮
【发布时间】:2016-02-16 19:34:46
【问题描述】:

我想抓取automatedtheboringstuff.com 并输出所有练习项目并将它们存储到一个文件中。最终,我希望我的代码从一页移动到下一个搜索练习项目的页面,但现在我只是想让beautifulsoup 将练习项目输出到一个页面上。但它不起作用。我只是得到一个空白 []。

这是我目前的代码。

def pageDownload(webPage):
    # Downloads the page

    while not url.endswith('appendixc'):
        print('Downloading page %s...' % url)
        res = requests.get(url)
        res.raise_for_status()

        soup = bs4.BeautifulSoup(res.text)

        return soup

def getProjects(downloadedPage):
    # gets the projects contents

    soup = downloadedPage
    book =  soup.find_all('p', attrs={'title' : 'Practice Projects'})
    print(book)

【问题讨论】:

  • 您在哪个页面上看到p 块将title 设置为Practice Projects? (我在网站上第一眼没看到)
  • 另外,你有一段时间内的回报 - 这是故意的吗?

标签: python python-3.x web-scraping beautifulsoup


【解决方案1】:

试试这个。

def pageDownload(webPage):
    # Downloads the page

    while not url.endswith('appendixc'):
        print('Downloading page %s...' % url)
        res = requests.get(url)
        res.raise_for_status()

        soup = bs4.BeautifulSoup(res.text)

        return soup

def getProjects(downloadedPage):
    # gets the projects contents

    soup = downloadedPage
    book =  soup.find_all('div', {'class': "book", 'title' : 'Practice Projects'})
    print(book)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2022-01-02
    • 2023-03-13
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 2016-01-09
    相关资源
    最近更新 更多