Selenium code is not able to scrape ofashion.com.cn

2024/11/20 14:15:46

I was building a web scraper by using python selenium. The script scraped sites like amazon, stack overflow and flipcart but wasn't able to scrape ofashion. It is always returning me a blank .csv file.

Here is my code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import pandas as pd
import timeuser_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) ' \'Chrome/80.0.3987.132 Safari/537.36'driver_exe = 'chromedriver'
options = Options()
#options.add_argument("--headless")
options.add_argument(f'user-agent={user_agent}')
options.add_argument("--disable-web-security")
options.add_argument("--allow-running-insecure-content")
options.add_argument("--allow-cross-origin-auth-prompt")driver = webdriver.Chrome(executable_path=r"C:\Users\intel\Downloads\Setups\chromedriver.exe", options=options)
driver.get("https://www.ofashion.com.cn/goods/10001?t=15777838840003")
class_Name = "." + "ellipsis-single ware-brand"
x = driver.find_elements_by_css_selector(class_Name.replace(' ','.'))
web_content_list = []for i in x:web_content_dict = {}web_content_dict["Title"] = i.textweb_content_list.append(web_content_dict)df = pd.DataFrame(web_content_list)
df.to_csv(r'C:\Users\intel\Desktop\data_file.csv',index=False, mode='a', encoding='utf-8')

Any help would be appreciated!

Answer

This is because the website is loaded through javascript. You see that loading sign (with the clothes hanger)? If you look at the top of the tab, you can see that the page isn't loading anymore. To wait for it to load completely, you can use Selenium's Wait Methods.

NOTE: Please put driver.close() at the end of your code to close the chromedriver window properly.

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

Related Q&A

How can I access each estimater in scikit-learn pipelines? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

How do I structure a repo with Cloud Run needing higher level code?

I have added code to a repo to build a Cloud Run service. The structure is like this:I want to run b.py in cr. Is there any way I can deploy cr without just copying b.py into the cr directory? (I dont…

Unable to build kivy image loaded .py file into exe using auto-py-to-exe

I have a simple kivy file in which i want to cover the entire canvas with an image bgi.jpg MainWidget: <MainWidget>:canvas.before:Rectangle:size:self.sizesource:bgi.jpgand the .py file code i…

Pandas Panel is deprecated,

This code snippet is from one of my script which works fine in current panda version (0.23) but Panel is deprecated and will be removed in a future version.panel = pd.Panel(dict(df1=dataframe1,df2=data…

Python - Why is this data being written to file incorrectly?

Only the first result is being written to a csv, with one letter of the url per row. This is instead of all urls being written, one per row.What am I not doing right in the last section of this code t…

How does Python interpreter look for types? [duplicate]

This question already has answers here:How does Python interpreter work in dynamic typing?(3 answers)Closed 10 months ago.If I write something like:>>> a = float()how does Python interpreter …

title() method in python writing functions when word like arent

using functiondef make_cap(sentence):return sentence.title()tryining outmake_cap("hello world") Hello World# it workd but when I have world like "arent" and isnt". how to write…

Creating a C++ Qt Gui for a Python logic

I was presented with a Python logic for which I need to create a GUI. I want to use Qt for that purpose and ideally I would like to program it in C++, without using the Qt Creator.What are recommended …

Pythons BaseHTTPServer returns junky responses

I use Pythons BaseHTTPServer and implement the following very simple BaseHTTPRequestHandler:class WorkerHandler(BaseHTTPRequestHandler):def do_GET(self):self.wfile.write({"status" : "rea…

Why is matplotlib failing on import matplotlib.pyplot as plt

I installed matplotlib using conda:conda install matplotlibThe following code failed:#!/usr/bin/env python import matplotlib import matplotlib.pyplot as pltWith this error message:"ImportError: N…