# coding=utf-8
import re

from DrissionPage import ChromiumPage
import time
from lxml import etree
import json
from langdetect import detect


class GoogleSearchQuestion():
    def __init__(self):

        self.page = ChromiumPage()
        self.file = open("google_question_" + str(time.strftime("%Y-%m-%d")) + ".json", "a", encoding="utf-8")
        self.file2 = open("google_question_html" + str(time.strftime("%Y-%m-%d")) + ".json", "a", encoding="utf-8")

    def get_keyword(self):
        keyword_list = ['resin']

        # g = open("google_question_2026-03-13.json", "r", encoding="utf-8")
        keyword_list1 = []
        # while True:
        #     # line = g.readline()
        #     if not line:
        #         break
        #     line_json = json.loads(line.strip("\n"))
        #     keyword = line_json.get("keyword")
        # keyword_list1.append('')
        keyword_list1 = list(set(keyword_list1))
        # print(len(keyword_list))
        keyword_list = list(set(keyword_list).difference(set(keyword_list1)))
        print(len(keyword_list))
        i = 1
        for keyword in keyword_list:
            try:
                self.get_relate_question(keyword)
            except Exception as e:
                print("出现异常，异常原因: " + str(e))
                time.sleep(10)
                continue
            print(f"已爬取到第{i}/{len(keyword_list)}个关键词:({keyword})")
            i += 1

    def get_relate_question(self, keyword):
        # self.page.get(f"https://www.google.com.hk/search?q={keyword}&lr=lang_en")
        self.page.get(f"https://www.google.com/search?hl=en&gl=us&lr=lang_en&q=resin")
        time.sleep(2)

        print(self.page.title)

        while True:
            res = self.page.html
            if re.search(
                    "我们的系统检测到您的计算机网络中存在异常流量。此网页用于确认这些请求是由您而不是自动程序发出的",
                    res):
                print("验证码界面")
                time.sleep(5)
            else:
                break

        origin_content = etree.HTML(res)
        q_list = origin_content.xpath(
            "//div[@id='search']//div[@jsname='yEVEwb']")
        if not q_list:
            return

        rank_num = 1
        data_list = []
        data_list2 = []
        for q_li in q_list:
            question = q_li.xpath(".//span[@class='CSkcDe']//text()")[0]

            str_html = etree.tostring(q_li, encoding='unicode', method='html')
            answer_divs = q_li.xpath(".//div[@class='wDYxhc']//span[@class='hgKElc']//text()")
            images = q_li.xpath(".//div[@class='XOEbc']//img/@data-src")
            urls = q_li.xpath(".//a[@jsname='UWckNb']/@href")
            if urls:
                url = urls[0]
            else:
                url = ""

            if images:
                image = images[0]
            else:
                image = ""

            if not answer_divs:
                answer_divs = q_li.xpath(".//div[@class='wDYxhc']//text()")

            if not answer_divs:
                continue
            text_str = "".join(answer_divs)
            text_str = text_str.replace("更多项…", "")
            data = {"question": question, "url": url, "answer": text_str, "image": image, "index": rank_num, "html": ""}
            data_list.append(data)

            rank_num += 1
        item = {"keyword": keyword, "paa": data_list}
        print(item)
        self.write_file(item)


    def get_image(self, url):
        pass

    def br_replace(self, text_obj):
        """"""
        # 使用XPath选择所有的<br>标签
        br_tags = text_obj.xpath('.//br')
        # 遍历选定的<br>标签，将它们替换为\n
        for br_tag in br_tags:
            if br_tag.tail:
                br_tag.tail = '\n' + br_tag.tail
            else:
                br_tag.tail = '\n'
        return text_obj

    def p_replace(self, text_obj):
        """"""
        # 使用XPath选择所有的<br>标签
        br_tags = text_obj.xpath('.//p|.//h1|.//h2|.//h3|.//h4|.//h5|.//h6|.//div')
        # 遍历选定的<br>标签，将它们替换为\n
        for p_tag in br_tags:
            if p_tag.xpath('.//br'):
                continue
            next_sibling = p_tag.xpath('following-sibling::*[1]')
            if next_sibling and next_sibling[0].tag == 'br':
                continue
            br_tag = etree.Element('br')
            p_tag.addnext(br_tag)
        return text_obj

    def remove_element(self, el):
        """"""
        parent = el.getparent()
        if el.tail:
            prev = el.getprevious()
            if prev:
                prev.tail = (prev.tail or '') + el.tail
            else:
                parent.text = (parent.text or '') + el.tail
        parent.remove(el)

    def write_file(self, item):
        data_str = json.dumps(dict(item), ensure_ascii=False) + "\n"
        self.file.write(data_str)

    def write_file2(self, item):
        data_str = json.dumps(dict(item), ensure_ascii=False) + "\n"
        self.file2.write(data_str)

    def run(self):
        self.get_keyword()


if __name__ == '__main__':

    google_search_keyword = GoogleSearchQuestion()
    google_search_keyword.run()
