我正在尝试编写一些代码来抓取网站以获取链接列表,然后我将使用这些链接执行其他操作。我找到了一些代码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())
关于python - Pandas 追加到系列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47610531/