[英]Pandas appending to series
我正在尝试编写一些代码来刮取网站的链接列表,然后我将使用它们进行其他操作。 我在这里找到了一些我要尝试适应的代码,以便将其添加到系列中,而不是打印列表。 我的代码如下:
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'])
传递网址时,出现以下错误
cannot concatenate a non-NDFrame object
知道我要去哪里错了吗?
Series
对象的.append()
方法需要另一个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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.