【问题标题】:Pandas appending to series附加到系列的熊猫
【发布时间】:2017-12-02 17:25:35
【问题描述】:

我正在尝试编写一些代码来抓取网站以获取链接列表,然后我将对其进行其他操作。我发现了一些代码here,我正在尝试对其进行调整,以便将其添加到系列中,而不是打印列表。我的代码如下:

import pandas as pd
from bs4 import BeautifulSoup
from urllib.parse import urljoin
user_agent = {'User-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0'}

linksList = pd.Series()

def process(url):
    r = requests.get(url, headers=user_agent)
    soup = BeautifulSoup(r.text, "lxml")

    for tag in soup.findAll('a', href=True):
        tag['href'] = urljoin(url, tag['href'])
        linksList.append(tag['href'])

当我传递一个 URL 时,我收到以下错误

cannot concatenate a non-NDFrame object

知道我哪里出错了吗?

【问题讨论】:

    标签: python pandas beautifulsoup


    【解决方案1】:

    .append() method of a Series object 期望另一个 Series 对象作为参数。也就是说,它用于将Series 连接在一起。

    在您的情况下,您可以将href 值收集到一个列表中并初始化一个Series

    def process(url):
        r = requests.get(url, headers=user_agent)
        soup = BeautifulSoup(r.text, "lxml")
    
        return [urljoin(url, tag['href']) for tag in soup.select('a[href]')]:
    
    links_list = pd.Series(process())
    

    【讨论】:

    • 谢谢你知道如何
    猜你喜欢
    • 2019-10-01
    • 2018-02-28
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 2015-06-16
    • 2016-08-19
    相关资源
    最近更新 更多