<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Python on Harry&#39;s Blog</title>
    <link>https://sudo-djug.xyz/tags/python/</link>
    <description>Recent content in Python on Harry&#39;s Blog</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Sun, 10 Apr 2022 00:00:00 +0800</lastBuildDate>
    <atom:link href="https://sudo-djug.xyz/tags/python/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Python爬虫踩坑记</title>
      <link>https://sudo-djug.xyz/posts/2022-04-10-python-web-scraper/</link>
      <pubDate>Sun, 10 Apr 2022 00:00:00 +0800</pubDate>
      <guid>https://sudo-djug.xyz/posts/2022-04-10-python-web-scraper/</guid>
      <description>&lt;h1 id=&#34;python爬虫踩坑记&#34;&gt;Python爬虫踩坑记&lt;/h1&gt;
&lt;p&gt;工作需要抓取一些公开数据，用 &lt;em&gt;Python&lt;/em&gt; 写了个简单的爬虫。一开始以为用 &lt;em&gt;requests&lt;/em&gt; 库直接 get 就行，结果发现很多网站都有反爬机制。&lt;/p&gt;
&lt;p&gt;第一个坑是 &lt;em&gt;User-Agent&lt;/em&gt; 被识别。解决方法是每次请求随机换一个：&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;headers = {&#39;User-Agent&#39;: &#39;Mozilla/5.0 ...&#39;}
response = requests.get(url, headers=headers)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;第二个坑是请求太频繁被 &lt;em&gt;IP&lt;/em&gt; 封禁。加了 &lt;em&gt;time.sleep(random.uniform(1, 3))&lt;/em&gt; 之后好了很多。&lt;/p&gt;
&lt;p&gt;第三个坑最麻烦，目标网站用了 &lt;em&gt;JavaScript&lt;/em&gt; 动态渲染，requests 抓到的 HTML 是空的。最后用了 &lt;em&gt;Selenium&lt;/em&gt; + &lt;em&gt;ChromeDriver&lt;/em&gt;，虽然慢了点，但能拿到完整数据。&lt;/p&gt;
&lt;h6 id=&#34;几点心得&#34;&gt;几点心得&lt;/h6&gt;
&lt;ul&gt;
&lt;li&gt;爬虫不是万能的，先看对方有没有提供 &lt;em&gt;API&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;遵守 &lt;em&gt;robots.txt&lt;/em&gt;，不要给人家服务器造成压力&lt;/li&gt;
&lt;li&gt;数据拿到之后记得清洗，原始数据往往很脏&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;写爬虫的过程也是学习 &lt;em&gt;HTTP&lt;/em&gt; 协议的过程，收获不小。&lt;/p&gt;</description>
    </item>
    <item>
      <title>Django初探</title>
      <link>https://sudo-djug.xyz/posts/2021-05-10-django-first-look/</link>
      <pubDate>Mon, 10 May 2021 00:00:00 +0800</pubDate>
      <guid>https://sudo-djug.xyz/posts/2021-05-10-django-first-look/</guid>
      <description>&lt;h1 id=&#34;django初探&#34;&gt;Django初探&lt;/h1&gt;
&lt;p&gt;今天跟随&lt;a href=&#34;https://www.dusaiphoto.com/article/2/&#34;&gt;杜赛老师的博客&lt;/a&gt;学习了一下 &lt;em&gt;Django&lt;/em&gt; 的基本用法，了解了MTV的一些基本概念，明白了搭建网站的基本步骤。&lt;/p&gt;
&lt;p&gt;回头继续学习。&lt;/p&gt;
&lt;p&gt;心得及要点如下：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;利用 &lt;em&gt;Pycharm&lt;/em&gt; 可以免于创建venv环境&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;每次修改完 &lt;em&gt;Model.py&lt;/em&gt; 都要更新数据库，命令是&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;1  
2  
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;|&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    python manage.py makemigrations  
    python manage.py migrate  
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;mdash;|&amp;mdash;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;要认真写代码，不要看错缩进和特殊符号，比如逗号、引号等&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;明天继续学习，每天一个小时，争取在五月学完 &lt;em&gt;Django&lt;/em&gt; 基本框架。&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
