News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

如何使用爬虫(Python篇)

posted on 2023-06-06 12:50     read(1025)     comment(0)     like(28)     collect(0)


A crawler (also known as a web crawler , web crawler) is a software system that automatically visits a website, and it is often used to crawl information on a website. Crawlers can be used to automatically discover new web pages when a website is updated, or when a website search engine index needs to be updated.

The workflow of a crawler is usually as follows:

  1. Starting from a web page, the crawler parses the HTML code of the web page and finds the links in it.

  1. The crawler continues to visit these links and parses the HTML code of new pages to find more links.

  1. This process is repeated until the crawler has crawled the entire site, or until a termination condition is reached.

Here is a simple tutorial on writing a crawler in Python:

  1. Install Python and crawler libraries.

To write a crawler in Python, you first need to install a Python interpreter. You can download the installation package from the Python official website, or use the package manager that comes with the system to install it.

Next, you need to install the crawler library. The most commonly used crawling library is Beautiful Soup, which can easily parse HTML and XML documents. Beautiful Soup can be installed with the following command:

pip install beautifulsoup4
  1. Import library.

Before using a bug library in Python code, you need to import the library first. When using the Beautiful Soup crawler, the library can be imported using the following code:

from bs4 import BeautifulSoup
  1. Get the HTML code.

The HTML code of the web pages that the crawler needs to crawl is stored on the web server. You can use Python's requests library to send an HTTP request to get the HTML code of a web page.

The sample code is as follows:

  1. import requests
  2. URL = "http://www.example.com"
  3. page = requests.get(URL)
  4. html_code = page.text
  1. Parses HTML code.

Parsing HTML code with Beautiful Soup makes it easy to extract information.

The sample code is as follows:

  1. soup = BeautifulSoup(html_code, 'html.parser')
  2. # 获取所有的链接
  3. links = soup.find_all('a')
  4. for link in links:
  5. print(link.get('href'))
  6. # 获取所有的段落
  7. paragraphs = soup.find_all('p')
  8. for paragraph in paragraphs:
  9. print(paragraph.text)
  1. Save information.

The crawled information can be stored in a file or database for later use.

The sample code is as follows:

  1. # 将信息存储到文件中
  2. with open('info.txt', 'w') as f:
  3. for paragraph in paragraphs:
  4. f.write(paragraph.text + '\n')
  5. # 将信息存储到数据库中
  6. import sqlite3
  7. conn = sqlite3.connect('info.db')
  8. cursor = conn.cursor()
  9. # 建表
  10. cursor.execute('''CREATE TABLE IF NOT EXISTS info (id INTEGER PRIMARY KEY, text TEXT)''')
  11. # 插入数据
  12. for i, paragraph in enumerate(paragraphs):
  13. cursor.execute('''INSERT INTO info (id, text) VALUES (?, ?)''', (i, paragraph.text))
  14. conn.commit()
  1. Handle exceptions.

During the crawling process, various abnormal situations may be encountered, such as network connection errors, non-existent web pages, server errors, etc. These exceptions should be handled in the crawler code to ensure the stability of the crawler.

The sample code is as follows:

  1. import requests
  2. URL = "http://www.example.com"
  3. try:
  4. page = requests.get(URL)
  5. html_code = page.text
  6. except requests.exceptions.RequestException as e:
  7. print(e)
  1. Limit crawl rate.

The crawling speed of the crawler can put pressure on the website. In order to avoid excessive occupation of website resources, you can set a delay in the crawler code to limit the crawling speed.

The sample code is as follows:

  1. import time
  2. time.sleep(1) # 延时 1 秒
  1. Anti-crawler mechanism.

Some websites use anti-crawler mechanisms to prevent crawlers from accessing. These mechanisms include disabling the crawler's IP address, using a CAPTCHA, setting a User-Agent, etc. The site's terms of use should be respected and these mechanisms considered in the crawler code.

The sample code is as follows:

  1. import requests
  2. URL = "http://www.example.com"
  3. headers = {
  4. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
  5. }
  6. page = requests.get(URL, headers=headers)
  7. html_code = page.text
  1. Crawl dynamic web pages.

The content of many web pages is dynamically generated through JavaScript. This kind of webpage is called a dynamic webpage. Crawlers cannot directly obtain the HTML code of dynamic web pages, and need to use special methods to crawl.

Commonly used methods are to use the browser console to debug the webpage, or use the API interface that comes with the webpage to obtain data.

  1. Encapsulate the crawler into a function .

When the code of the crawler is complex, the crawler can be encapsulated into a function. In this way, the crawler can be called conveniently, and the crawler code can be reused in different programs.

The sample code is as follows:

  1. def crawl(url):
  2. """爬取网页内容"""
  3. page = requests.get(url)
  4. html_code = page.text
  5. soup = BeautifulSoup(html_code, 'html.parser')
  6. paragraphs = soup.find_all('p')
  7. return [paragraph.text for paragraph in paragraphs]

The code to call the crawler function is as follows:

info = crawl("http://www.example.com")

A crawler is a powerful tool that can help us obtain a large amount of information. However, crawlers must also abide by network ethics, and crawlers cannot be abused. The copyright and privacy rights of the website should be respected to avoid crawling unauthorized information.

When writing a crawler, there are some considerations and optimization methods to consider:

  • The crawling speed and efficiency of crawlers can be improved through multi-threaded or distributed crawlers.

  • Comments should be added to the crawler code to facilitate future reading and maintenance.

  • During the crawling process, excessive pressure on the website should be avoided. You can set a delay, or use a proxy server.

  • When crawling a website, the robots.txt file of the website should be obeyed. This file is used to tell the crawler which pages are not allowed to crawl.

  • Crawlers can be implemented in many languages, not just Python. You can choose the appropriate language according to your needs and your own skills.

  1. Crawl AJAX web pages.

AJAX web pages are a technique for dynamically updating web pages using JavaScript. Crawlers cannot directly obtain the content of AJAX web pages, and need to use special methods to crawl.

Commonly used methods are to use the browser console to debug the webpage, or use the API interface that comes with the webpage to obtain data.

  1. Use a proxy server.

When the crawling speed of the crawler is too fast, the IP address may be blocked by the website. To avoid this, a proxy server can be used. Proxy servers can help crawlers change IP addresses to avoid being blocked.

The code to use the proxy server is as follows:

  1. import requests
  2. URL = "http://www.example.com"
  3. proxies = {
  4. 'http': 'http://192.168.0.1:8080',
  5. 'https': 'https://192.168.0.1:8080'
  6. }
  7. page = requests.get(URL, proxies=proxies)
  1. Optimize crawlers with preprocessing libraries.

When the task of the crawler is more complicated, the preprocessing library can be used to optimize the crawler. The preprocessing library can help crawlers process large amounts of data efficiently, and can provide various convenient functions.

Commonly used preprocessing libraries include NumPy, pandas, etc.

  1. Use caching to speed up crawlers.

During the crawling process, the crawler may visit the same web page multiple times. In order to reduce unnecessary network requests, a cache mechanism can be used to speed up crawlers.

The cache mechanism can store the crawled webpages locally so that they can be used directly the next time they are crawled. This can greatly reduce the number of network requests and improve the efficiency of crawlers.

The code to use the cache is as follows:

  1. import requests
  2. import os
  3. URL = "http://www.example.com"
  4. # 将网页存储到本地
  5. def save_page(url, filename):
  6. if not os.path.exists(filename): # 如果文件不存在
  7. page = requests.get(url)
  8. with open(filename, 'w') as f:
  9. f.write(page.text)
  10. # 读取本地网页
  11. def read_page(filename):
  12. with open(filename, 'r') as f:
  13. return f.read()
  14. # 使用缓存
  15. def get_page(url):
  16. filename = url.split('/')[-1] + '.html'
  17. if not os.path.exists(filename):
  18. save_page(url, filename)
  19. return read_page(filename)
  20. html_code = get_page(URL)
  1. Use multithreading to speed up crawlers.

When crawling web pages, you can use multi-threading to speed up the crawler. Multi-threading allows crawlers to crawl multiple web pages at the same time, thereby improving efficiency.

The code using multithreading is as follows:

  1. import threading
  2. import time
  3. URLs = [
  4. "http://www.example.com/1",
  5. "http://www.example.com/2",
  6. "http://www.example.com/3",
  7. ]
  8. # 爬取网页
  9. def crawl(url):
  10. page = requests.get(url)
  11. html_code = page.text
  12. # 多线程爬取
  13. threads = []
  14. for url in URLs:
  15. t = threading.Thread(target=crawl, args=(url,))
  16. threads.append(t)
  17. t.start()
  18. # 等待线程结束
  19. for t in threads:
  20. t.join()

Note: When using multithreading, you need to pay attention to thread safety issues.

  1. Use distributed crawlers to speed up crawling.

When crawling a large amount of data, you can use a distributed crawler method to speed up crawling. Distributed crawlers can use multiple machines to crawl web pages at the same time, thereby greatly improving the efficiency of crawlers.

The code for using distributed crawlers is as follows:

  1. from multipyparallel import Client
  2. # 连接到本地的 IPython 并行服务器
  3. rc = Client()
  4. # 爬取网页
  5. def crawl(url):
  6. page = requests.get(url)
  7. html_code = page.text
  8. # 并行爬取
  9. result = rc[:].apply_async(crawl, URLs)
  10. # 等待爬取完成
  11. result.wait()

At the end, I hope everyone can like, follow or reward me.

Your encouragement will be the greatest motivation for my creation! ! !



Category of website: technical article > Blog

Author:Fiee

link:http://www.pythonblackhole.com/blog/article/80657/05d7816d4c468a7ee5c0/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

28 0
collect article
collected

Comment content: (supports up to 255 characters)