Derrick jamal
Derrick jamal

Reputation: 47

How do you add a new line in an xpath expression?

Given the following html content :

<div>
  <h3>Name :</h3>
  <p>Person A</p>
  <h3>Name :</h3>
  <p>Person B</p>
  <h3>Name :</h3>
  <p>Person c</p>
</div>

I need to extract the name of every person under the p tag using xPath. When I use the following expression :

name = container.xpath(".//h3[text()='Name :']/following-sibling::p/text()") 

I get this output in a .csv file I am extracting:

Person A Person B Person C

But I need to have line breaks after every person, like this:

Person A
Person B
Person C

The code I use to get the csv file is as below:

with open("person.csv", "w") as f:
    writer = csv.DictWriter(f, fieldnames = fieldnames, lineterminator = '\n')
    writer.writeheader()
    for row in output:
        writer.writerow(row)

Is there a way I can structure my xPath in order to achieve that?

Upvotes: 1

Views: 246

Answers (1)

Jack Fleeting
Jack Fleeting

Reputation: 24930

Try something like this:

name = container.xpath(".//h3[text()='Name :']/following-sibling::p/text()") 
names = ''
for n in name:
    names+=(n+'\n')

and use names in your output before you save to csv.

Upvotes: 1

Related Questions