【发布时间】:2020-05-01 19:05:43
【问题描述】:
我有一个 MongoDB 数据库,其中包含一个名为“links”的数组的文档,其中包含在上一步中抓取的每个网页的一个元素。还包括作为网站的 ID 和域。
这是一个示例文档:
{
"_id": {
"$oid": "5eabc5085ad3f439d5dc6940"
},
"domain": "http://www.linnlanes.com/",
"links": ["http://www.linnlanes.com/index.html", "http://www.linnlanes.com/ContactUS/contactus.htm", "http://www.linnlanes.com/HoursRates/rates.htm", "http://www.linnlanes.com/Food/Menu.htm", "http://www.linnlanes.com/BowlingSpecials/specials.htm", "http://www.linnlanes.com/BirthdaySpecials/parties.htm", "http://www.linnlanes.com/ProShop/proshop.htm", "http://www.linnlanes.com/Tournaments/tournaments.htm", "http://www.lebanonusbc.org/Leagues/leagues.htm", "http://www.LebanonUSBC.org/", "http://www.linnlanes.com/BowlingSpecials/tacotuesday.htm", "http://www.linnlanes.com/BowlingSpecials/niteowl.htm"]
}
我正在尝试通过 pymongo 和 pandas 将数据拉入 Python Dataframe。我的示例代码如下:
import pymongo
import pandas as pd
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["crawler"]
mycol = mydb["links"]
myquery = {"links": {"$regex": u"contact"}}
df = pd.DataFrame(mycol.find(myquery))
print(df['links'])
当我运行上面的代码时,我会得到所有在其“链接”元素之一中具有“联系人”的文档。问题是它正在拉入“链接”的每个元素。我只想获取包含“联系人”的元素。除了所示的正则表达式之外,我对执行“like”功能的其他方法持开放态度。
我得到了什么:
0 [http://www.linnlanes.com/index.html, http://w...
Name: links, Length: 414, dtype: object
我想得到什么:
0 [http://www.linnlanes.com/ContactUS/contactus.htm]
Name: links, Length: 414, dtype: object
【问题讨论】:
标签: arrays python-3.x pandas dataframe pymongo-3.x