首頁 後端開發 Python教學 使用Python的Scrapy框架编写web爬虫的简单示例

使用Python的Scrapy框架编写web爬虫的简单示例

Jun 10, 2016 pm 03:14 PM
python

 在这个教材中,我们假定你已经安装了Scrapy。假如你没有安装,你可以参考这个安装指南。

我们将会用开放目录项目(dmoz)作为我们例子去抓取。

这个教材将会带你走过下面这几个方面:

  •     创造一个新的Scrapy项目
  •     定义您将提取的Item
  •     编写一个蜘蛛去抓取网站并提取Items。
  •     编写一个Item Pipeline用来存储提出出来的Items

Scrapy由Python写成。假如你刚刚接触Python这门语言,你可能想要了解这门语言起,怎么最好的利用这门语言。假如你已经熟悉其它类似的语言,想要快速地学习Python,我们推荐这种深入方式学习Python。假如你是新手,想从开始使用Python学习,可以尝试去看看非程序员Python资源列表。

创造一个项目

在你要抓取之前,首先要建立一个新的Scrapy项目。然后进去你的存放代码目录,执行如下命令。

scrapy startproject tutorial

登入後複製

它将会创建如下的向导目录:

复制代码 代码如下:

tutorial/
scrapy.cfg
tutorial/
__init__.py
items.py
pipelines.py
settings.py
spiders/
__init__.py
...

这是一些基本信息:

  • scrapy.cfg: 项目的配置文件。
  • tutorial/: 项目的python模块, 在这里稍后你将会导入你的代码。
  • tutorial/items.py: 项目items文件。
  • tutorial/pipelines.py: 项目管道文件。
  • tutorial/settings.py: 项目配置文件。
  • tutorial/spiders/: 你将要放入你的spider到这个目录中。


定义我们的Item

Items是装载我们抓取数据的容器。它们工作像简单的Python字典,它提供更多的保护,比如对未定义的字段提供填充功能防止出错。

它们通过创建scrapy.item.Item类来声明并定义它们的属性作为scrapy.item.Field 对象,就像是一个对象关系映射(假如你不熟悉ORMs,你将会看见它是一个简单的任务).

我们将需要的item模块化,来控制从demoz.org网站获取的数据,比如我们将要去抓取网站的名字,url和描述信息。我们定义这三种属性的域。我们编辑items.py文件,它在向导目录中。我们Item类看起来像这样。


from scrapy.item import Item, Field
 
class DmozItem(Item):
 title = Field()
 link = Field()
 desc = Field()
登入後複製

这个看起来复杂的,但是定义这些item能让你用其他Scrapy组件的时候知道你的item到底是什么

我们第一个Spider

Spiders是用户写的类,它用来去抓取一个网站的信息(或者一组网站) 。
我们定义一个初始化的URLs列表去下载,如何跟踪链接,如何去解析这些页面的内容去提取 items.创建一个Spider,你必须是scrapy.spider.BaseSpider的子类, 并定义三个主要的,强制性的属性。

名字: Spider的标识. 它必须是唯一的, 那就是说,你不能在不同的Spiders中设置相同的名字。

开始链接:Spider将会去爬这些URLs的列表。所以刚开始的下载页面将要包含在这些列表中。其他子URL将会从这些起始URL中继承性生成。

parse() 是spider的一个方法, 调用时候传入从每一个URL传回的Response对象作为参数。response是方法的唯一参数。

这个方法负责解析response数据和提出抓取的数据(作为抓取的items),跟踪URLs

parse()方法负责处理response和返回抓取数据(作为Item对象) 和跟踪更多的URLs(作为request的对象)

这是我们的第一个Spider的代码;它保存在moz/spiders文件夹中,被命名为dmoz_spider.py:

from scrapy.spider import BaseSpider
 
class DmozSpider(BaseSpider):
 name = "dmoz"
 allowed_domains = ["dmoz.org"]
 start_urls = [
  "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
  "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
 ]
 
 def parse(self, response):
  filename = response.url.split("/")[-2]
  open(filename, 'wb').write(response.body)
登入後複製

为了使你的spider工作, 到项目的顶级目录让后运行:

scrapy crawl dmoz

登入後複製

crawl dmoz命令使spider去爬dmoz.org网站的信息。你将会得到如下类似的信息:

2008-08-20 03:51:13-0300 [scrapy] INFO: Started project: dmoz
2008-08-20 03:51:13-0300 [tutorial] INFO: Enabled extensions: ...
2008-08-20 03:51:13-0300 [tutorial] INFO: Enabled downloader middlewares: ...
2008-08-20 03:51:13-0300 [tutorial] INFO: Enabled spider middlewares: ...
2008-08-20 03:51:13-0300 [tutorial] INFO: Enabled item pipelines: ...
2008-08-20 03:51:14-0300 [dmoz] INFO: Spider opened
2008-08-20 03:51:14-0300 [dmoz] DEBUG: Crawled <http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/> (referer: <None>)
2008-08-20 03:51:14-0300 [dmoz] DEBUG: Crawled <http://www.dmoz.org/Computers/Programming/Languages/Python/Books/> (referer: <None>)
2008-08-20 03:51:14-0300 [dmoz] INFO: Spider closed (finished)

登入後複製

注意那些行包含[dmoz], 它和我们的spider相关。你能够看见每行初始化的URL日志信息。因为这些URLs是起始页面,所以他们没有引用referrers。 所以在每行的末尾部门,你能看见(referer: ).

但是有趣的是,在我们的parse方法作用下,两个文件被创建: Books and Resources, 它保航两个URLs的内容
刚刚发生了什么事情?

Scrapy为每一个start_urls创建一个scrapy.http.Request对象,并将爬虫的parse 方法指定为回调函数。

这些Request首先被调度,然后被执行,之后通过parse()方法,将scrapy.http.Response对象被返回,结果也被反馈给爬虫。

提取Items
选择器介绍

我们有多种方式去提取网页中数据。Scrapy 使用的是XPath表达式,通常叫做XPath selectors。如果想了解更多关于选择器和提取数据的机制,可以看看如下教程XPath selectors documentation.

这里有一些表达式的例子和它们相关的含义:

  • /html/head/title: 选择元素,在HTML文档的<head>元素里</li> <li> /html/head/title/text(): 选择<title>元素里面的文本</li> <li> //td: 选择所有的<td>元素</li> <li> //div[@class="mine"]: 选择所有的div元素里面class属性为mine的</li> </ul> <p>这里有许多的例子关于怎么使用XPath,可以说XPath表达式是非常强大的。如果你想要学习更多关于XPath,我们推荐如下教程this XPath tutorial.</p> <p>为了更好使用XPaths, Scrapy提供了一个XPathSelector类,它有两种方式, HtmlXPathSelector(HTML相关数据)和XmlXPathSelector(XML相关数据)。如果你想使用它们,你必须实例化一个Response对象.</p> <p>你能够把selectors作为对象,它代表文件结构中的节点。所以,第1个实例的节点相当于root节点,或者称为整个文档的节点。</p> <p>选择器有三种方法(点击方法你能够看见完整的API文档)。</p> <ul> <li> select(): 返回选择器的列表,每一个select表示一个xpath表达式选择的节点。</li> <li> extract(): 返回一个unicode字符串 ,该字符串XPath选择器返回的数据。</li> <li> re() : 返回unicode字符串列表,字符串作为参数由正则表达式提取出来。</li> </ul> <p><br /> <strong>在Shell里面使用选择器</strong></p> <p>为了更加形象的使用选择器,我们将会使用Scrapy shell,它同时需要你的系统安装IPython (一个扩展的Python控制台)。</p> <p>如果使用shell,你必须到项目的顶级目录上,让后运行如下命令:</p> <p>scrapy shell http://www.dmoz.org/Computers/Programming/Languages/Python/Books/</p> <p>shell将会显示如下的信息</p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> [ ... Scrapy log here ... ] [s] Available Scrapy objects: [s] 2010-08-19 21:45:59-0300 [default] INFO: Spider closed (finished) [s] hxs <HtmlXPathSelector (http://www.dmoz.org/Computers/Programming/Languages/Python/Books/) xpath=None> [s] item Item() [s] request <GET http://www.dmoz.org/Computers/Programming/Languages/Python/Books/> [s] response <200 http://www.dmoz.org/Computers/Programming/Languages/Python/Books/> [s] spider <BaseSpider 'default' at 0x1b6c2d0> [s] xxs <XmlXPathSelector (http://www.dmoz.org/Computers/Programming/Languages/Python/Books/) xpath=None> [s] Useful shortcuts: [s] shelp() Print this help [s] fetch(req_or_url) Fetch a new request or URL and update shell objects [s] view(response) View response in a browser In [1]: </pre><div class="contentsignin">登入後複製</div></div> </p> <p>当shell装载之后,你将会得到一个response的本地变量。所以你输入reponse.body,你能够看见response的body部分或者你能够输入response.headers,你能够看见reponse.headers部分。</p> <p>shell同样实例化了两个选择器,一个是HTML(在hvx变量里),一个是XML(在xxs变量里)。所以我们尝试怎么使用它们:</p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> In [1]: hxs.select('//title') Out[1]: [<HtmlXPathSelector (title) xpath=//title>] In [2]: hxs.select('//title').extract() Out[2]: [u'<title>Open Directory - Computers: Programming: Languages: Python: Books</title>'] In [3]: hxs.select('//title/text()') Out[3]: [<HtmlXPathSelector (text) xpath=//title/text()>] In [4]: hxs.select('//title/text()').extract() Out[4]: [u'Open Directory - Computers: Programming: Languages: Python: Books'] In [5]: hxs.select('//title/text()').re('(\w+):') Out[5]: [u'Computers', u'Programming', u'Languages', u'Python'] </pre><div class="contentsignin">登入後複製</div></div> </p> <p><strong>提取数据Extracting the data</strong></p> <p>现在我们开始尝试在这几个页面里提取真正的信息。</p> <p>你能够在控制台里面输入response.body,检查源代码里面的XPaths是否与预期相同。然而,检查原始的HTML代码是一件非常枯燥乏味的事情。假如你想让你的工作变的简单,你使用Firefox扩展的插件例如Firebug来做这项任务。更多关于介绍信息请看Using Firebug for scraping和Using Firefox for scraping。</p> <p>当你检查了页面源代码之后,你将会发现页面的信息放在一个<ul>元素里面,事实上,确切地说是第二个<ul>元素。</p> <p>所以我们选择每一个<li>元素使用如下的代码:<br /> <br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> hxs.select('//ul/li') </pre><div class="contentsignin">登入後複製</div></div> </p> <p>网站的描述信息可以使用如下代码:<br /> <br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> hxs.select('//ul/li/text()').extract() </pre><div class="contentsignin">登入後複製</div></div> </p> <p>网站的标题:<br /> <br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> hxs.select('//ul/li/a/text()').extract() </pre><div class="contentsignin">登入後複製</div></div> </p> <p>网站的链接:<br /> <br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> hxs.select('//ul/li/a/@href').extract() </pre><div class="contentsignin">登入後複製</div></div> </p> <p>如前所述,每个select()调用返回一个selectors列表,所以我们可以结合select()去挖掘更深的节点。我们将会用到这些特性,所以:<br /> <br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> sites = hxs.select('//ul/li') for site in sites: title = site.select('a/text()').extract() link = site.select('a/@href').extract() desc = site.select('text()').extract() print title, link, desc Note </pre><div class="contentsignin">登入後複製</div></div> </p> <p><br /> 如果想了解更多的嵌套选择器,可以参考Nesting selectors和Working with relative XPaths相关的Selectors文档<br /> 将代码添加到我们spider中:</p> <p> <br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> from scrapy.spider import BaseSpider from scrapy.selector import HtmlXPathSelector class DmozSpider(BaseSpider): name = "dmoz" allowed_domains = ["dmoz.org"] start_urls = [ "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/", "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/" ] def parse(self, response): hxs = HtmlXPathSelector(response) sites = hxs.select('//ul/li') for site in sites: title = site.select('a/text()').extract() link = site.select('a/@href').extract() desc = site.select('text()').extract() print title, link, desc </pre><div class="contentsignin">登入後複製</div></div> </p> <p>现在我们再次抓取dmoz.org,你将看到站点在输出中被打印 ,运行命令:<br /> <br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> scrapy crawl dmoz </pre><div class="contentsignin">登入後複製</div></div> </p> <p><strong>使用我们的 item</strong></p> <p>Item对象是自定义python字典;使用标准字典类似的语法,你能够访问它们的字段(就是以前我们定义的属性) <br /> <br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> >>> item = DmozItem() >>> item['title'] = 'Example title' >>> item['title'] 'Example title' </pre><div class="contentsignin">登入後複製</div></div> </p> <p>Spiders希望将抓取的数据放在 Item对象里。所以,为了返回我们抓取的数据,最终的代码要如下这么写 :</p> <p> <br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> from scrapy.spider import BaseSpider from scrapy.selector import HtmlXPathSelector from tutorial.items import DmozItem class DmozSpider(BaseSpider): name = "dmoz" allowed_domains = ["dmoz.org"] start_urls = [ "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/", "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/" ] def parse(self, response): hxs = HtmlXPathSelector(response) sites = hxs.select('//ul/li') items = [] for site in sites: item = DmozItem() item['title'] = site.select('a/text()').extract() item['link'] = site.select('a/@href').extract() item['desc'] = site.select('text()').extract() items.append(item) return items </pre><div class="contentsignin">登入後複製</div></div> <strong>Note<br /> </strong></p> <p>你能够找到完整功能的spider在dirbot项目里,同样你可以访问https://github.com/scrapy/dirbot</p> <p>现在重新抓取dmoz.org网站:</p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> [dmoz] DEBUG: Scraped from <200 http://www.dmoz.org/Computers/Programming/Languages/Python/Books/> {'desc': [u' - By David Mertz; Addison Wesley. Book in progress, full text, ASCII format. Asks for feedback. [author website, Gnosis Software, Inc.\n], 'link': [u'http://gnosis.cx/TPiP/'], 'title': [u'Text Processing in Python']} [dmoz] DEBUG: Scraped from <200 http://www.dmoz.org/Computers/Programming/Languages/Python/Books/> {'desc': [u' - By Sean McGrath; Prentice Hall PTR, 2000, ISBN 0130211192, has CD-ROM. Methods to build XML applications fast, Python tutorial, DOM and SAX, new Pyxie open source XML processing library. [Prentice Hall PTR]\n'], 'link': [u'http://www.informit.com/store/product.aspx&#63;isbn=0130211192'], 'title': [u'XML Processing with Python']} </pre><div class="contentsignin">登入後複製</div></div> </p> <p><strong><br /> 存储抓取的数据</strong></p> <p>最简单的方式去存储抓取的数据是使用Feed exports,使用如下的命令:</p> <p> <br /> </p> <div class="jb51code"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> scrapy crawl dmoz -o items.json -t json </pre><div class="contentsignin">登入後複製</div></div> <p>它将会产生一个items.json文件,它包含所有抓取的items(序列化的JSON)。</p> <p>在一些小的项目里(例如我们的教程中),那就足够啦。然而,假如你想要执行更多复杂的抓取items,你能够写一个 Item Pipeline。 因为在项目创建的时候,一个专门用于Item Pipelines的占位符文件已经随着项目一起被建立,目录在tutorial/pipelines.py。如果你只需要存取这些抓取后的items的话,就不需要去实现任何的条目管道。 <br> </p> </div> </div> </div> <div class="wzconShengming_sp"> <div class="bzsmdiv_sp">本網站聲明</div> <div>本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn</div> </div> </div> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="2507867629"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div class="AI_ToolDetails_main4sR"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5902227090019525" data-ad-slot="3653428331" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <!-- <div class="phpgenera_Details_mainR4"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hotarticle2.png" alt="" /> <h2>熱門文章</h2> </div> <div class="phpgenera_Details_mainR4_bottom"> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796789525.html" title="Windows 11 KB5054979中的新功能以及如何解決更新問題" class="phpgenera_Details_mainR4_bottom_title">Windows 11 KB5054979中的新功能以及如何解決更新問題</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>3 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796793874.html" title="如何修復KB5055523無法在Windows 11中安裝?" class="phpgenera_Details_mainR4_bottom_title">如何修復KB5055523無法在Windows 11中安裝?</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>2 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796787760.html" title="Inzoi:如何申請學校和大學" class="phpgenera_Details_mainR4_bottom_title">Inzoi:如何申請學校和大學</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>4 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796793871.html" title="如何修復KB5055518無法在Windows 10中安裝?" class="phpgenera_Details_mainR4_bottom_title">如何修復KB5055518無法在Windows 10中安裝?</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>2 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796786383.html" title="Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla" class="phpgenera_Details_mainR4_bottom_title">Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>4 週前</span> <span>By 尊渡假赌尊渡假赌尊渡假赌</span> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/zh-tw/article.html">顯示更多</a> </div> </div> </div> --> <div class="phpgenera_Details_mainR3"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hottools2.png" alt="" /> <h2>熱AI工具</h2> </div> <div class="phpgenera_Details_mainR3_bottom"> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/ai/undresserai-undress" title="Undresser.AI Undress" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411540686492.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Undresser.AI Undress" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/ai/undresserai-undress" title="Undresser.AI Undress" class="phpmain_tab2_mids_title"> <h3>Undresser.AI Undress</h3> </a> <p>人工智慧驅動的應用程序,用於創建逼真的裸體照片</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/ai/ai-clothes-remover" title="AI Clothes Remover" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411552797167.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="AI Clothes Remover" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/ai/ai-clothes-remover" title="AI Clothes Remover" class="phpmain_tab2_mids_title"> <h3>AI Clothes Remover</h3> </a> <p>用於從照片中去除衣服的線上人工智慧工具。</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/ai/undress-ai-tool" title="Undress AI Tool" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173410641626608.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Undress AI Tool" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/ai/undress-ai-tool" title="Undress AI Tool" class="phpmain_tab2_mids_title"> <h3>Undress AI Tool</h3> </a> <p>免費脫衣圖片</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/ai/clothoffio" title="Clothoff.io" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411529149311.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Clothoff.io" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/ai/clothoffio" title="Clothoff.io" class="phpmain_tab2_mids_title"> <h3>Clothoff.io</h3> </a> <p>AI脫衣器</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/ai/video-swap" title="Video Face Swap" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173414504068133.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Video Face Swap" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/ai/video-swap" title="Video Face Swap" class="phpmain_tab2_mids_title"> <h3>Video Face Swap</h3> </a> <p>使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!</p> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/zh-tw/ai">顯示更多</a> </div> </div> </div> <script src="https://sw.php.cn/hezuo/cac1399ab368127f9b113b14eb3316d0.js" type="text/javascript"></script> <div class="phpgenera_Details_mainR4"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hotarticle2.png" alt="" /> <h2>熱門文章</h2> </div> <div class="phpgenera_Details_mainR4_bottom"> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796789525.html" title="Windows 11 KB5054979中的新功能以及如何解決更新問題" class="phpgenera_Details_mainR4_bottom_title">Windows 11 KB5054979中的新功能以及如何解決更新問題</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>3 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796793874.html" title="如何修復KB5055523無法在Windows 11中安裝?" class="phpgenera_Details_mainR4_bottom_title">如何修復KB5055523無法在Windows 11中安裝?</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>2 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796787760.html" title="Inzoi:如何申請學校和大學" class="phpgenera_Details_mainR4_bottom_title">Inzoi:如何申請學校和大學</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>4 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796793871.html" title="如何修復KB5055518無法在Windows 10中安裝?" class="phpgenera_Details_mainR4_bottom_title">如何修復KB5055518無法在Windows 10中安裝?</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>2 週前</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/1796786383.html" title="Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla" class="phpgenera_Details_mainR4_bottom_title">Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>4 週前</span> <span>By 尊渡假赌尊渡假赌尊渡假赌</span> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/zh-tw/article.html">顯示更多</a> </div> </div> </div> <div class="phpgenera_Details_mainR3"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hottools2.png" alt="" /> <h2>熱工具</h2> </div> <div class="phpgenera_Details_mainR3_bottom"> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/92" title="記事本++7.3.1" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58ab96f0f39f7357.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="記事本++7.3.1" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/92" title="記事本++7.3.1" class="phpmain_tab2_mids_title"> <h3>記事本++7.3.1</h3> </a> <p>好用且免費的程式碼編輯器</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/93" title="SublimeText3漢化版" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58ab97a3baad9677.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="SublimeText3漢化版" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/93" title="SublimeText3漢化版" class="phpmain_tab2_mids_title"> <h3>SublimeText3漢化版</h3> </a> <p>中文版,非常好用</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/121" title="禪工作室 13.0.1" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58ab97ecd1ab2670.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="禪工作室 13.0.1" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/121" title="禪工作室 13.0.1" class="phpmain_tab2_mids_title"> <h3>禪工作室 13.0.1</h3> </a> <p>強大的PHP整合開發環境</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/469" title="Dreamweaver CS6" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58d0e0fc74683535.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="Dreamweaver CS6" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/469" title="Dreamweaver CS6" class="phpmain_tab2_mids_title"> <h3>Dreamweaver CS6</h3> </a> <p>視覺化網頁開發工具</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/500" title="SublimeText3 Mac版" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58d34035e2757995.png?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="SublimeText3 Mac版" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/zh-tw/toolset/development-tools/500" title="SublimeText3 Mac版" class="phpmain_tab2_mids_title"> <h3>SublimeText3 Mac版</h3> </a> <p>神級程式碼編輯軟體(SublimeText3)</p> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/zh-tw/ai">顯示更多</a> </div> </div> </div> <div class="phpgenera_Details_mainR4"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hotarticle2.png" alt="" /> <h2>熱門話題</h2> </div> <div class="phpgenera_Details_mainR4_bottom"> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/gmailyxdlrkzn" title="gmail信箱登陸入口在哪裡" class="phpgenera_Details_mainR4_bottom_title">gmail信箱登陸入口在哪裡</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>7831</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>15</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/java-tutorial" title="Java教學" class="phpgenera_Details_mainR4_bottom_title">Java教學</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>1648</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>14</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/cakephp-tutor" title="CakePHP 教程" class="phpgenera_Details_mainR4_bottom_title">CakePHP 教程</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>1402</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>52</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/laravel-tutori" title="Laravel 教程" class="phpgenera_Details_mainR4_bottom_title">Laravel 教程</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>1300</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>25</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/zh-tw/faq/php-tutorial" title="PHP教程" class="phpgenera_Details_mainR4_bottom_title">PHP教程</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>1240</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>29</span> </div> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/zh-tw/faq/zt">顯示更多</a> </div> </div> </div> </div> </div> <div class="Article_Details_main2"> <div class="phpgenera_Details_mainL4"> <div class="phpmain1_2_top"> <a href="javascript:void(0);" class="phpmain1_2_top_title">Related knowledge<img src="/static/imghw/index2_title2.png" alt="" /></a> </div> <div class="phpgenera_Details_mainL4_info"> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/1796797869.html" title="PHP和Python:解釋了不同的範例" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/001/253/068/174490716137257.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="PHP和Python:解釋了不同的範例" /> </a> <a href="https://www.php.cn/zh-tw/faq/1796797869.html" title="PHP和Python:解釋了不同的範例" class="phphistorical_Version2_mids_title">PHP和Python:解釋了不同的範例</a> <span class="Articlelist_txts_time">Apr 18, 2025 am 12:26 AM</span> <p class="Articlelist_txts_p">PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/1796797864.html" title="在PHP和Python之間進行選擇:指南" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/001/253/068/174490706146904.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="在PHP和Python之間進行選擇:指南" /> </a> <a href="https://www.php.cn/zh-tw/faq/1796797864.html" title="在PHP和Python之間進行選擇:指南" class="phphistorical_Version2_mids_title">在PHP和Python之間進行選擇:指南</a> <span class="Articlelist_txts_time">Apr 18, 2025 am 12:24 AM</span> <p class="Articlelist_txts_p">PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/1796796853.html" title="Python vs. JavaScript:學習曲線和易用性" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/001/253/068/174473354083140.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="Python vs. JavaScript:學習曲線和易用性" /> </a> <a href="https://www.php.cn/zh-tw/faq/1796796853.html" title="Python vs. JavaScript:學習曲線和易用性" class="phphistorical_Version2_mids_title">Python vs. JavaScript:學習曲線和易用性</a> <span class="Articlelist_txts_time">Apr 16, 2025 am 12:12 AM</span> <p class="Articlelist_txts_p">Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/1796797866.html" title="PHP和Python:深入了解他們的歷史" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/001/253/068/174490710066424.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="PHP和Python:深入了解他們的歷史" /> </a> <a href="https://www.php.cn/zh-tw/faq/1796797866.html" title="PHP和Python:深入了解他們的歷史" class="phphistorical_Version2_mids_title">PHP和Python:深入了解他們的歷史</a> <span class="Articlelist_txts_time">Apr 18, 2025 am 12:25 AM</span> <p class="Articlelist_txts_p">PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/1796796708.html" title="vs code 可以在 Windows 8 中運行嗎" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/202412/31/2024123119425980275.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="vs code 可以在 Windows 8 中運行嗎" /> </a> <a href="https://www.php.cn/zh-tw/faq/1796796708.html" title="vs code 可以在 Windows 8 中運行嗎" class="phphistorical_Version2_mids_title">vs code 可以在 Windows 8 中運行嗎</a> <span class="Articlelist_txts_time">Apr 15, 2025 pm 07:24 PM</span> <p class="Articlelist_txts_p">VS Code可以在Windows 8上運行,但體驗可能不佳。首先確保系統已更新到最新補丁,然後下載與系統架構匹配的VS Code安裝包,按照提示安裝。安裝後,注意某些擴展程序可能與Windows 8不兼容,需要尋找替代擴展或在虛擬機中使用更新的Windows系統。安裝必要的擴展,檢查是否正常工作。儘管VS Code在Windows 8上可行,但建議升級到更新的Windows系統以獲得更好的開發體驗和安全保障。</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/1796796735.html" title="visual studio code 可以用於 python 嗎" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/202412/27/2024122713580165301.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="visual studio code 可以用於 python 嗎" /> </a> <a href="https://www.php.cn/zh-tw/faq/1796796735.html" title="visual studio code 可以用於 python 嗎" class="phphistorical_Version2_mids_title">visual studio code 可以用於 python 嗎</a> <span class="Articlelist_txts_time">Apr 15, 2025 pm 08:18 PM</span> <p class="Articlelist_txts_p">VS Code 可用於編寫 Python,並提供許多功能,使其成為開發 Python 應用程序的理想工具。它允許用戶:安裝 Python 擴展,以獲得代碼補全、語法高亮和調試等功能。使用調試器逐步跟踪代碼,查找和修復錯誤。集成 Git,進行版本控制。使用代碼格式化工具,保持代碼一致性。使用 Linting 工具,提前發現潛在問題。</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/1796797229.html" title="notepad 怎麼運行python" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/202405/29/2024052914541688364.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="notepad 怎麼運行python" /> </a> <a href="https://www.php.cn/zh-tw/faq/1796797229.html" title="notepad 怎麼運行python" class="phphistorical_Version2_mids_title">notepad 怎麼運行python</a> <span class="Articlelist_txts_time">Apr 16, 2025 pm 07:33 PM</span> <p class="Articlelist_txts_p">在 Notepad 中運行 Python 代碼需要安裝 Python 可執行文件和 NppExec 插件。安裝 Python 並為其添加 PATH 後,在 NppExec 插件中配置命令為“python”、參數為“{CURRENT_DIRECTORY}{FILE_NAME}”,即可在 Notepad 中通過快捷鍵“F6”運行 Python 代碼。</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/zh-tw/faq/1796796719.html" title="vscode 擴展是否是惡意的" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/202412/27/2024122714102618732.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="vscode 擴展是否是惡意的" /> </a> <a href="https://www.php.cn/zh-tw/faq/1796796719.html" title="vscode 擴展是否是惡意的" class="phphistorical_Version2_mids_title">vscode 擴展是否是惡意的</a> <span class="Articlelist_txts_time">Apr 15, 2025 pm 07:57 PM</span> <p class="Articlelist_txts_p">VS Code 擴展存在惡意風險,例如隱藏惡意代碼、利用漏洞、偽裝成合法擴展。識別惡意擴展的方法包括:檢查發布者、閱讀評論、檢查代碼、謹慎安裝。安全措施還包括:安全意識、良好習慣、定期更新和殺毒軟件。</p> </div> </div> <a href="https://www.php.cn/zh-tw/be/" class="phpgenera_Details_mainL4_botton"> <span>See all articles</span> <img src="/static/imghw/down_right.png" alt="" /> </a> </div> </div> </div> </main> <footer> <div class="footer"> <div class="footertop"> <img src="/static/imghw/logo.png" alt=""> <p>公益線上PHP培訓,幫助PHP學習者快速成長!</p> </div> <div class="footermid"> <a href="https://www.php.cn/zh-tw/about/us.html">關於我們</a> <a href="https://www.php.cn/zh-tw/about/disclaimer.html">免責聲明</a> <a href="https://www.php.cn/zh-tw/update/article_0_1.html">Sitemap</a> </div> <div class="footerbottom"> <p> © php.cn All rights reserved </p> </div> </div> </footer> <input type="hidden" id="verifycode" value="/captcha.html"> <script>layui.use(['element', 'carousel'], function () {var element = layui.element;$ = layui.jquery;var carousel = layui.carousel;carousel.render({elem: '#test1', width: '100%', height: '330px', arrow: 'always'});$.getScript('/static/js/jquery.lazyload.min.js', function () {$("img").lazyload({placeholder: "/static/images/load.jpg", effect: "fadeIn", threshold: 200, skip_invisible: false});});});</script> <script src="/static/js/common_new.js"></script> <script type="text/javascript" src="/static/js/jquery.cookie.js?1745951475"></script> <script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script> <link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css?2' type='text/css' media='all' /> <script type='text/javascript' src='/static/js/viewer.min.js?1'></script> <script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script> <script type="text/javascript" src="/static/js/global.min.js?5.5.53"></script> <script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function () { var u = "https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u + 'matomo.php']); _paq.push(['setSiteId', '9']); var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0]; g.async = true; g.src = u + 'matomo.js'; s.parentNode.insertBefore(g, s); })(); </script> <script> // top layui.use(function () { var util = layui.util; util.fixbar({ on: { mouseenter: function (type) { layer.tips(type, this, { tips: 4, fixed: true, }); }, mouseleave: function (type) { layer.closeAll("tips"); }, }, }); }); document.addEventListener("DOMContentLoaded", (event) => { // 定义一个函数来处理滚动链接的点击事件 function setupScrollLink(scrollLinkId, targetElementId) { const scrollLink = document.getElementById(scrollLinkId); const targetElement = document.getElementById(targetElementId); if (scrollLink && targetElement) { scrollLink.addEventListener("click", (e) => { e.preventDefault(); // 阻止默认链接行为 targetElement.scrollIntoView({ behavior: "smooth" }); // 平滑滚动到目标元素 }); } else { console.warn( `Either scroll link with ID '${scrollLinkId}' or target element with ID '${targetElementId}' not found.` ); } } // 使用该函数设置多个滚动链接 setupScrollLink("Article_Details_main1L2s_1", "article_main_title1"); setupScrollLink("Article_Details_main1L2s_2", "article_main_title2"); setupScrollLink("Article_Details_main1L2s_3", "article_main_title3"); setupScrollLink("Article_Details_main1L2s_4", "article_main_title4"); setupScrollLink("Article_Details_main1L2s_5", "article_main_title5"); setupScrollLink("Article_Details_main1L2s_6", "article_main_title6"); // 可以继续添加更多的滚动链接设置 }); window.addEventListener("scroll", function () { var fixedElement = document.getElementById("Article_Details_main1Lmain"); var scrollTop = window.scrollY || document.documentElement.scrollTop; // 兼容不同浏览器 var clientHeight = window.innerHeight || document.documentElement.clientHeight; // 视口高度 var scrollHeight = document.documentElement.scrollHeight; // 页面总高度 // 计算距离底部的距离 var distanceToBottom = scrollHeight - scrollTop - clientHeight; // 当距离底部小于或等于300px时,取消固定定位 if (distanceToBottom <= 980) { fixedElement.classList.remove("Article_Details_main1Lmain"); fixedElement.classList.add("Article_Details_main1Lmain_relative"); } else { // 否则,保持固定定位 fixedElement.classList.remove("Article_Details_main1Lmain_relative"); fixedElement.classList.add("Article_Details_main1Lmain"); } }); </script> <script> document.addEventListener('DOMContentLoaded', function() { const mainNav = document.querySelector('.Article_Details_main1Lmain'); const header = document.querySelector('header'); if (mainNav) { window.addEventListener('scroll', function() { const scrollPosition = window.scrollY; if (scrollPosition > 84) { mainNav.classList.add('fixed'); } else { mainNav.classList.remove('fixed'); } }); } }); </script> </body> </html>