When scraping all the div to get the data getting the null list using lxml in python

2024/10/6 17:20:36

I want to scrape the product title , product link , product price but when I am using the xpath it is showing the null list . How to add the xpath and for loop to get the above details . I have tried this

import requests
import lxml.htmlhtml =  requests.get("https://www.lazada.sg/catalog/? q=Samsung+Mobile&_keyori=ss&from=input&spm=a2o42.home.search.go.654346b52P3y8Y")
doc = lxml.html.fromstring(html.content)
#print(doc)new = doc.xpath('//div[@class ="index__box___1Ffv-"]')
print(len(new))
for node in new:title = node.xpath('//* [@id="root"]/div/div[2]/div[1]/div/div[1]/div[2]/div[1]/div/div/div[2]/div[2]/text()')print(len(title))print(title)

I need to get all the product details as mention above. I want this by using only lxml library.

Answer

The main problem you are running into is that the information you want is not in doc. To see what you're working with, try something like:

f = open('title_dump.html', 'wb')
f.write(html.content)

Open it in a browser or text editor, and you'll notice that titles, prices and so on are missing. Most likely, they are being filled in by Javascript. As such, this may not be possible using only lxml.

https://en.xdnf.cn/q/118926.html

Related Q&A

Python how to convert this for loop into a while loop [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Converting a for loop to a while loop I have this for a for loop which I made I was wondering how I would write so it woul…

Joining elements in Python list

Given a string, say s=135 and a list, say A=[1,2,3,4,5,6,7], how can I separate the values in the list that are also in s (a digit of s) from the other elements and concatenate these other elements. Th…

Python - Count letters in random strings

I have a bunch of integers which are allocated values using the random module, then converted to letters depending on their position of the alphabet.I then combine a random sample of these variables in…

Looping until a specific key is pressed [duplicate]

This question already has answers here:detect key press in python, where each iteration can take more than a couple of seconds?(4 answers)Closed 2 years ago.I was trying to make a while loop which wou…

Getting sub-dataframe after sorting and groupby

I have a dataframe dfas:Election Year Votes Vote % Party Region 0 2000 42289 29.40 Janata Dal (United) A 1 2000 27618 19.20 Rashtriya Ja…

Use .vss stencil file to generate shapes by python code (use .vdx?)

I want to have my python program generate visio drawings using shapes from a stencil (.vss) file. How can I do that? I think I could generate the xml formatted .vdx file, but there isnt a lot of docum…

How can I capture detected image of object Yolov3 and display in flask

I am working on Real Time Object Detection using YOLOv3 with OpenCV and Python. Its works well. Currently I try to capture detected image of object and display in flask. Do someone know how to implemen…

ValueError: Shapes (2, 1) and () are incompatible

I have to use Tensorflow 0.11 for this code repo and this is the error I get:(py35) E:\opensource_codes\gesture_recognition\hand3d-master>python run.py Traceback (most recent call last):File "r…

Subtotals for Pandas pivot table index and column

Id like to add subtotal rows for index #1 (ie. Fruits and Animal) and subtotal columns for columns (ie. 2015 and 2016).For the subtotal columns, I could do something like this, but it seems inefficient…

Create two new columns derived from original columns in Python

I have a dataframe, df, that contains two columns that contain quarter values. I would like to create two more columns that are the equivalent "long dates". Data ID Quarter Delivery A …