【发布时间】:2021-10-22 23:18:37
【问题描述】:
我已经从 API 中提取数据,我正在遍历所有内容并找到一个键:具有 URL 的值。所以我正在创建一个单独的 URL 列表,我需要做的是点击链接并从页面中获取内容,将该页面的内容拉回数组/列表(它只是一段文本) 并且当然会遍历剩余的 URL。我是否需要使用 Selenium 或 BS4?如何循环访问并将页面内容拉入我的数组/列表?
json looks like this:
{
"merchandiseData": [
{
"clientID": 3003,
"name": "Yasir Carter",
"phone": "(758) 564-5345",
"email": "leo.vivamus@pedenec.net",
"address": "P.O. Box 881, 2723 Elementum, St.",
"postalZip": "DX2I 2LD",
"numberrange": 10,
"name1": "Harlan Mccarty",
"constant": ".com",
"text": "deserunt",
"url": "https://www.deserunt.com",
"text": "https://www."
},
]
}
到目前为止的代码:
import requests
import json
import pandas as pd
import sqlalchemy as sq
import time
from datetime import datetime, timedelta
from flatten_json import flatten# read file
with open('_files/TestFile2.json', 'r') as f:
file_contents = json.load(f)
allThis = []
for x in file_contents['merchandiseData']:
holdAllThis = {
'client_id' : x['clientID'],
'client_description_link' : x['url']
}
allThis.append(holdAllThis)
print(client_id, client_description_link)
print(allThis)
【问题讨论】:
-
allThis列表有客户端 id 和 url 吗?这是示例 json 吗?如果没有,_files/TestFile2.json中有多少 JSON 数组?您是否只想获取每个 URL 并从 UI 中提取一些文本? -
如果 URL 不同,您应该编写不同的代码来抓取每个 URL。是使用 bs4 还是 selenium,这取决于网页 - 如果页面是由 JavaScript 加载的,则使用 selenium,否则 bs4 就可以了。
-
嗨@cruisepandey。我在此处附加了 json 文件的示例:{github.com/webdevr712/python_follow_links.git} 当上面的脚本运行时,我得到了 clientID 和 url 的列表,是的,文件是 json。然后我想获取该列表,针对它运行一个 python 脚本去抓取每个 url,并将内容拉回一个数组,
-
嗨@Ram - 不确定我是否跟随。这些网址大约有 10K,所以我不能为每个网址设置单独的代码。脚本需要循环,
-
从您的 JSON 数据中,我看到 URL 不同。您打算如何抓取所有这些 URL?顺便说一句,你想从这些 URL 中提取什么数据?
标签: json python-3.x selenium beautifulsoup