文章目录
分享文章
# -*- coding:utf-8 -*-
import requests
import re
from lxml.html import etree
import time
import csv
from lxml import html
import htmlmin
url = "https://www.winsen-sensor.com/product/"
def get_one_page(url):
try:
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
res = requests.get(url=url, headers=headers)
res.encoding = 'utf-8'
if res.status_code == 200:
return res.text
else:
return None
except Exception:
return None
content = get_one_page(url)
tree = etree.HTML(content)
product_urls = tree.xpath('//div[@class="col-md-6 col-lg-3 isotope-item mb-0 pb-0"]/a/@href')
try:
with open('WooCommerce_Product.csv', 'a', encoding='utf-8', newline='') as f:
for url in product_urls:
content = get_one_page(url)
tree = etree.HTML(content)
name = tree.xpath('//h1/text()')[0]
print(name)
Short_description = tree.xpath('//div[@class="container"]//div[@class="border-radius overflow-hidden"]')[0]
Short_description_html = html.tostring(Short_description).decode("utf-8")
Short_description_html = htmlmin.minify(Short_description_html, remove_empty_space=True)
Description = tree.xpath('//div[@class="container"]//div[@class="col-lg-8 mb-5 mb-lg-0 appear-animation"]')[
0]
Description_html = html.tostring(Description).decode("utf-8")
Description_html = htmlmin.minify(Description_html, remove_empty_space=True)
Description_html = Description_html.replace("""<h4 class="text-6 pb-2">Technical Support:</h4><div class="custom-accordion-style-1 custom-accordion-style-1-grey accordion accordion-modern pb-2 mb-4" id=FAQAccordion><div class="card card-default"><div class=card-header><h4 class="card-title m-0"><a class="accordion-toggle text-color-dark font-weight-semibold text-2-5 collapsed" data-bs-toggle=collapse href=#collapseFAQOne> 1- Do you accept Customized Products? </a></h4></div><div id=collapseFAQOne class=collapse data-bs-parent=#FAQAccordion><div class="card-body ps-4"><p class=mb-0>Customized technical design and function, LOGO and package are available without extra charge (including molding fee) for mass volume.</p></div></div></div><div class="card card-default"><div class=card-header><h4 class="card-title m-0"><a class="accordion-toggle text-color-dark font-weight-semibold text-2-5 collapsed" data-bs-toggle=collapse href=#collapseFAQTwo> 2- Is there Sleeping Mode for the sensors? </a></h4></div><div id=collapseFAQTwo class=collapse data-bs-parent=#FAQAccordion><div class="card-body ps-4"><p class=mb-0>The sensors don't have sleeping mode. It can be set in program if needed.</p></div></div></div><div class="card card-default"><div class=card-header><h4 class="card-title m-0"><a class="accordion-toggle text-color-dark font-weight-semibold text-2-5 collapsed" data-bs-toggle=collapse href=#collapseFAQFour> 3- How do you guarantee the products quality before outgoing?? </a></h4></div><div id=collapseFAQFour class=collapse data-bs-parent=#FAQAccordion><div class="card-body ps-4"><p class=mb-0>All Winsen products go through strict inspections and tests to make sure they are 100% qualified before shipping out.</p></div></div></div><div class="card card-default"><div class=card-header><h4 class="card-title m-0"><a class="accordion-toggle text-color-dark font-weight-semibold text-2-5 collapsed" data-bs-toggle=collapse href=#collapseFAQFive> 4- Are the sensors calibrated before outgoing? </a></h4></div><div id=collapseFAQFive class=collapse data-bs-parent=#FAQAccordion><div class="card-body ps-4"><p class=mb-0>The sensor has been fully tested (not calibrated) before leaving the factory. However, after transportation and storage, the environment conditions change, and the surface of the sensor absorbs moisture, mixed gas, pollution matters, etc. It is recommended that customers perform aging and calibration after sensors become stable according to the program and use occasions of the whole device.</p></div></div></div><div class="card card-default"><div class=card-header><h4 class="card-title m-0"><a class="accordion-toggle text-color-dark font-weight-semibold text-2-5 collapsed" data-bs-toggle=collapse href=#collapseFAQTSix> 5- What’s the influence of temperature and humidity on sensor performance and life? </a></h4></div><div id=collapseFAQTSix class=collapse data-bs-parent=#FAQAccordion><div class="card-body ps-4"><p class=mb-0>15﹪-90﹪RH without condensation,greater or less than this range or long-term high temperature and high humidity will affect the life of the sensor.</p></div></div></div><div class="card card-default"><div class=card-header><h4 class="card-title m-0"><a class="accordion-toggle text-color-dark font-weight-semibold text-2-5 collapsed" data-bs-toggle=collapse href=#collapseFAQThree> 6- What are the certificates that Winsen sensors have been awarded? </a></h4></div><div id=collapseFAQThree class=collapse data-bs-parent=#FAQAccordion><div class="card-body ps-4"><p class=mb-0>Our sensors are certified with UL, CE, 3C, ROHS, anti-explosion, fire prevention, and coal mining safety according to different applications. And we provide assistance for new certification.</p></div></div></div><div class="card card-default"><div class=card-header><h4 class="card-title m-0"><a class="accordion-toggle text-color-dark font-weight-semibold text-2-5 collapsed" data-bs-toggle=collapse href=#collapseFAQSeven> 7- What's wrong with the sensor failing to respond? </a></h4></div><div id=collapseFAQSeven class=collapse data-bs-parent=#FAQAccordion><div class="card-body ps-4"><p class=mb-0>Many factors can cause the sensor to fail such as broken wires in transportation, poisoning, falling off of the sensitive material, damage to the heating wire (device), etc. Please find specific cause in each category with Winsen’s aftersales.</p></div></div></div></div><p class="pb-2 mb-4">Winsen provides professional consultation and service before and after sales. We will recommend the best fitting sensor for your demand. If you have any problem, don’t hesitate to email us to <a href=mailto:[email protected]>[email protected]</a></p><p class=mb-0>Due to high volume of daily incoming emails and time zone difference, we may not be able to reply your emails immediately. But we will reply to your inquiry in 24 business hours.</p>""",'')
Published = "1"
Visibility_in_catalog = "visible"
Images = tree.xpath('//div[@class="lightbox"]//img/@src')[0]
Images_url = "https://www.winsen-sensor.com" + Images
Type = "Simple"
Categories = "CO Sensor"
SKU = url.split('/')[-1]
SKU = SKU[0:-5]
lines = [name, SKU, Short_description_html, Description_html, Published, Visibility_in_catalog, Images_url, Type,
Categories]
writer = csv.writer(f)
writer.writerow(lines)
except Exception as e:
print(url)
pass