我正在尝试从包含JSON
数据的链接中抓取数据,这是代码:
import requests
import json
parameters = ['a:1','a:2','a:3','a:4','a:3','a:4','a:5','a:6','a:7','a:8','a:9','a:10',]
for item in parameters:
key, value = item.split(':')[0], item.split(':')[1]
url = "https://xxxx.000webhostapp.com/getNamesEnc02Motasel2.php?keyword=%s&type=2&limit=%s" %(key, value)
r = requests.get(url)
cont = json.loads(r.content)
print(cont)
输出类似于
[{'name': 'Absz', 'phone': '66343212'}, {'name': 'ddd ', 'phone': '545432211'}, {'name': 'ezd' 'phone':'54856886'}]
我想将所有数据存储在CSV
文件中。
我该怎么做?
此外,如您所见,我正在使用参数列表执行多个请求,但是我认为有一种方法可以循环1 to 200
中的limit参数,而无需在参数中键入每个关键字和数字。
谢谢。
答案 0 :(得分:0)
尝试以下代码,它将逐行创建csv:
if __name__== '__main__':
n = list(input())
print(n)
print(sorted(n))
希望这能回答您的问题!
答案 1 :(得分:0)
import requests
import json
import pandas as pd
parameters = ['a:1','a:2','a:3','a:4','a:3','a:4','a:5','a:6','a:7','a:8','a:9','a:10']
results = pd.DataFrame()
for item in parameters:
key, value = item.split(':')
url = "https://xxxx.000webhostapp.com/getNamesEnc02Motasel2.php?keyword=%s&type=2&limit=%s" %(key, value)
r = requests.get(url)
cont = json.loads(r.content)
temp_df = pd.DataFrame(cont)
results = results.append(temp_df)
results.to_csv('path/to/filename.csv', index=False)
答案 2 :(得分:0)
我个人使用了三种将数据抓取为 CSV 文件的技术。
源代码:
import bs4
import requests
from fake_useragent import UserAgent
import csv
## initializing the UserAgent object
user_agent = UserAgent()
url = "https://www.consumerreports.org/cro/a-to-z-index/products/index.htm"
## getting the reponse from the page using get method of requests module
page = requests.get(url, headers={"user-agent": user_agent.chrome})
## storing the content of the page in a variable
html = page.content
## creating BeautifulSoup object
soup = bs4.BeautifulSoup(html, "html.parser")
## div tags with crux-body-copy class
div_class = "crux-body-copy"
## getting all the divs with class 'crux-body-copy'
div_tags = soup.find_all("div", class_="div_class")
## then we open a csv file in append mode
with open("product_data.csv", "a") as csv_file:
writer = csv.writer(csv_file)
## extracting the names and links from the div tags
for tag in div_tags:
name = tag.a.text.strip()
link = tag.a['href']
## now we will write data to the file
writer.writerow([name, link])