Warm tip: This article is reproduced from stackoverflow.com, please click
web-scraping xpath

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

发布于 2020-04-11 22:38:17

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?

Questioner
Derrick jamal
Viewed
85
Jack Fleeting 2020-02-04 20:22

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.