Skip to main content
 首页 » 编程设计

python之如何在新列中保存正在读取 pandas read_html() 函数的 url

2024年10月17日9lyj

我对从网站中提取一些表格很感兴趣,我定义了表格所在的链接列表。每个链接都有几个列数相同的表格。因此,我将链接列表中的所有表提取到带有 pandas 的单个表中 read_html()功能如下:

links = ['url1.com','url2.com',...,'urlN.com'] 
 
import multiprocessing 
def process_url(link): 
    return pd.concat(pd.read_html(link), ignore_index=False) # add in a new column the link where the table was extracted.. 
 
p = multiprocessing.Pool() 
df = pd.concat(p.map(process, links), ignore_index=True) 

我注意到执行每个表的出处链接会很有帮助(即保存在一个新列中,链接来自最终表的行)。因此,我的问题是,如何执行 pandas read_html()新专栏中的引用链接?

例如:

表 1 和表 2 在 url1.com 中:

表 1:

fruit, color, season, price                       
apple, red, winter, 2$ 
watermelon, green, winter, 3$ 
orange, orange, spring, 1$ 

表 2:

fruit, color, season, price 
peppermint, green, fall, 3$ 
pear, yellow, fall, 4$       

表 3 位于 url2.com 中

fruit, color, season, price  
tomato, red, fall, 3$ 
pumpking, orange, fall, 1$ 

我想在新列中保存每个表被提取的位置(即在新列中执行表的引用):

  fruit, color, season, price, link         
0 apple, red, winter, 2$, url1.com              
1 watermelon, green, winter, 3$, url1.com 
2 orange, orange, spring, 1$, url1.com 
3 peppermint, green, fall, 3$, url1.com 
4 pear, yellow, fall, 4$, url1.com 
5 tomato, red, fall, 3$, url2.com 
6 pumpking, orange, fall, 1$, url2.com 

另一个例子是这个“图表”,注意table1和table2在url1.com中。另一方面,表 3 在 url2.com 中。使用上述功能,我从不同链接中的表创建了一个表,我的目标是创建一个符合表提取位置的列(只是为了保存引用):

source: url1.com 
 
fruit, color, season, price                       
apple, red, winter, 2$ 
watermelon, green, winter, 3$ 
orange, orange, spring, 1$ 
 
source: url1.com 
 
fruit, color, season, price 
peppermint, green, fall, 3$ 
pear, yellow, fall, 4$             
                                 ---->      fruit, color, season, price, link         
                                            apple, red, winter, 2$, url1.com              
                                            watermelon, green, winter, 3$, url1.com  
                                            orange, orange, spring, 1$, url1.com 
                                            peppermint, green, fall, 3$, url1.com 
                                            pear, yellow, fall, 4$, url1.com 
                                            tomato, red, fall, 3$, url2.com 
source: url2.com                            pumpking, orange, fall, 1$, url1.com 
fruit, color, season, price  
tomato, red, fall, 3$ 
pumpking, orange, fall, 1$ 

知道怎么做吗?

请您参考如下方法:

这应该可以解决问题:

def process_url(link): 
    return pd.concat(pd.read_html(link), ignore_index=False).assign(link=link) 

解释:DataFrame.assign(new_column=expression)将向您的 DF 添加一个新的虚拟列。

演示:

In [2]: d1 
Out[2]: 
   a   b 
0  1  10 
1  2  20 
 
In [3]: d2 
Out[3]: 
    a    b 
0  11  100 
1  12  200 
 
In [4]: link = 'http://url1.com' 
 
In [5]: pd.concat([d1, d2], ignore_index=True).assign(link=link) 
Out[5]: 
    a    b             link 
0   1   10  http://url1.com 
1   2   20  http://url1.com 
2  11  100  http://url1.com 
3  12  200  http://url1.com