简体   繁体   中英

Python lxml print each table row

When I enter something in 'cn', script will make query on website and give me table with multiple rows

from lxml import html
from lxml import etree
from lxml.etree import XPath
import requests

cn = input ('CN: ')

find_page = requests.get('search query' + cn + '')
tree = html.fromstring(find_page.content)

# //tr[2]/td[2]/a/text() is first row after <th>
com = tree.xpath('//tr[2]/td[2]/a/text()')

print ('COM:', com)

This code print me only first row from table on XPath location //tr[2] but I need to print all other table rows //tr[3]/td[2]/a/text() //tr[4]/td[2]/a/text() //tr[...]/td[2]/a/text()

EDIT:

After solving to get all items from table, I have result for example COM: ['DAP', 'DAPA', 'DAP FOOD'] all of this have href. I can access and scrape only on first link (DAP) but can't scrape from (DAPA and DAP FOOD)

from lxml import html
from lxml import etree
from lxml.etree import XPath
import requests

cn = input ('CN: ')

find_page = requests.get('search query' + cn + '')
tree = html.fromstring(find_page.content)
    
# //tr[2]/td[2]/a/text() is first row after <th>
com = tree.xpath('//tr/td[2]/a/text()')

link = tree.xpath('//tr/td[2]/a/@href')[0]
link = str(link)

com_link = ('website' + link)
page = requests.get(com_link)
tree = html.fromstring(page.content)

postal_code = tree.xpath('//span[@itemprop="postalCode"]/text()')[0]

print ('COM:', com)
print ('Postal Code', postal_code)

How can I access on DAP, DAPA, DAP FOOD and get postal_code from each?

Changed com = tree.xpath('//tr[2]/td[2]/a/text()') to com = tree.xpath('//tr/td[2]/a/text()') and it working

from lxml import html
from lxml import etree
from lxml.etree import XPath
import requests

cn = input ('CN: ')

find_page = requests.get('search query' + cn + '')
tree = html.fromstring(find_page.content)

# //tr[2]/td[2]/a/text() is first row after <th>
com = tree.xpath('//tr/td[2]/a/text()')

print ('COM:', com)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM