I am collecting names and numbers and exporting it into a csv file.
column A is = names column B is = numbers
How can I get column c to fill all rows with “Phoenix” so that every column that has a name or number in it column c would say phoenix?
JavaScript
x
29
29
1
import csv
2
3
import requests
4
from bs4 import BeautifulSoup
5
6
7
realtor_data = []
8
9
for page in range(1, 6):
10
print(f"Scraping page {page}...")
11
url = f"https://www.realtor.com/realestateagents/phoenix_az/pg-{page}"
12
soup = BeautifulSoup(requests.get(url).text, "html.parser")
13
14
for agent_card in soup.find_all("div", {"class": "agent-list-card clearfix"}):
15
name = agent_card.find("div", {"class": "agent-name text-bold"}).find("a")
16
number = agent_card.find("div", {"itemprop": "telephone"})
17
realtor_data.append(
18
[
19
name.getText().strip(),
20
number.getText().strip() if number is not None else "N/A"
21
22
],
23
)
24
25
with open("data.csv", "w") as output:
26
w = csv.writer(output)
27
w.writerow(["NAME:", "PHONE NUMBER:", "CITY:"])
28
w.writerows(realtor_data)
29
Advertisement
Answer
There is the columns c with phoenix.
JavaScript
1
37
37
1
import csv
2
3
import requests
4
from bs4 import BeautifulSoup
5
6
7
realtor_data = []
8
9
for page in range(1, 6):
10
print(f"Scraping page {page}...")
11
url = f"https://www.realtor.com/realestateagents/phoenix_az/pg-{page}"
12
soup = BeautifulSoup(requests.get(url).text, "html.parser")
13
14
for agent_card in soup.find_all("div", {"class": "agent-list-card clearfix"}):
15
name = agent_card.find("div", {"class": "agent-name text-bold"}).find("a")
16
number = agent_card.find("div", {"itemprop": "telephone"})
17
realtor_data.append(
18
[
19
name.getText().strip(),
20
number.getText().strip() if number is not None else "N/A"
21
22
],
23
)
24
25
with open("data.csv", "w") as output:
26
w = csv.writer(output)
27
w.writerow(["NAME:", "PHONE NUMBER:", "CITY:"])
28
w.writerows(realtor_data)
29
30
import pandas as pd
31
a=pd.read_csv("data.csv")
32
a2 = a.iloc[:,[0,1]]
33
a3 = a.iloc[:,[2]]
34
a3 = a3.fillna("phoenix")
35
b=pd.concat([a2,a3],axis=1)
36
b.to_csv("data.csv")
37