熊猫追加到系列

我想写一些代码来刮一个网站的链接列表,然后我会做一些事情之后。我发现一些代码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 

任何想法,我错了?

回答:

.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())

以上是 熊猫追加到系列 的全部内容, 来源链接: utcz.com/qa/257491.html

回到顶部