Pythonの爬虫類 - 猫の目の映画TOP100をクロールライブラリ使用要求

猫の目の映画TOPl100の関連するコンテンツをつかむために、ライブラリの使用を要求。

標的部位ます。https://maoyan.com/board/4

1.キャプチャホーム

Get_one_pageは、メソッドを定義し、そして彼に渡すURLパラメータを与える
キャッツアイムービーサイトには、ヘッダーがクロールすることができます設定した後、抗爬虫類の対策があります注意してください


import requests
headers = {
    'Content-Type': 'text/plain; charset=UTF-8',
    'Origin': 'https://maoyan.com',
    'Referer': 'https://maoyan.com/board/4',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}


def get_one_page(url, headers):
    try:
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            return response.text
        return None
    except RequestsException:
        return None
def main():
	 url = "https://maoyan.com/board/4"
	 html = get_one_page(url, headers)
	 print(html)

if __name__ == '__main__':
      main()

ソースコードを実行しているが見つかり成功
ここに画像を挿入説明

2.通常のエキス

ここでは、デフparse_one_pageメソッドを定義します

import requests
import re
from requests.exceptions import RequestException
headers = {
    'Content-Type': 'text/plain; charset=UTF-8',
    'Origin': 'https://maoyan.com',
    'Referer': 'https://maoyan.com/board/4',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}


def get_one_page(url, headers):
    try:
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            return response.text
        return None
    except RequestsException:
        return None

def parse_one_page(html):
   pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a'
                        + '.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',
                        re.S)
   items = re.findall(pattern, html)
   for item in items:
      yield {
         'index': item[0],
         'image': item[1],
         'title': item[2],
         'actor': item[3].strip()[3:],
         'time': item[4].strip()[5:],
         'score': item[5] + item[6]
      }

def main():
    url = "https://maoyan.com/board/4"
    html = get_one_page(url, headers)
    for item in parse_one_page(html):
        print(item)

if __name__ == '__main__':
    main()

業績
ここに画像を挿入説明

3.ライト・ファイル

私たちは、ファイルに書き込まれた結果を抽出し、テキストファイルに直接ここに書き込みます。

import requests
import re
import json
from requests.exceptions import RequestException
headers = {
    'Content-Type': 'text/plain; charset=UTF-8',
    'Origin': 'https://maoyan.com',
    'Referer': 'https://maoyan.com/board/4',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}


def get_one_page(url, headers):
    try:
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            return response.text
        return None
    except RequestsException:
        return None

def parse_one_page(html):
   pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a'
                        + '.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',
                        re.S)
   items = re.findall(pattern, html)
   for item in items:
      yield {
         'index': item[0],
         'image': item[1],
         'title': item[2],
         'actor': item[3].strip()[3:],
         'time': item[4].strip()[5:],
         'score': item[5] + item[6]
      }

def write_to_file(content):
   with open('result.txt', 'a', encoding='utf-8') as f:
      f.write(json.dumps(content, ensure_ascii=False) + '\n')
      f.close()

def main():
    url = "https://maoyan.com/board/4"
    html = get_one_page(url, headers)
    for item in parse_one_page(html):
        print(item)
        write_to_file(item)

if __name__ == '__main__':
    main()

正常に書き込まれた文書のFOUND
ここに画像を挿入説明

6.タブクロール

我々はそのTOP100ムービーをつかむ必要があるため、上記の手順が成功したと、最初のページにランキング映画をクロールし、我々はまた、他の90枚のフィルムをクロール実現するために必要なことがわかりました。

サイトの分析を通じ、URLや変更が発生しているものを観察するためにページの内容は
ここに画像を挿入説明
ここに画像を挿入説明
**にページのURLで見つけることができます。https:?//Maoyan.corn/board/4 1以上前のURLパラメータより、= 10をオフセット= 10をオフセットし、その結果、現在20にフィルム11のランキングことを示すことを、我々はこのオフセットパラメータであると結論づけました。次に、[次へ]をクリックし、AにページのURLを見つけるhttp://maoyan.com/board/4?= 20オフセット、オフセットパラメータが20になり、その結果はフィルムが21-30位にランクことを示しています。なお、オフセット量は、フィルムIDは、N + L N + 10、表示ページ10に表示され、Nであれば、意味をオフセット値をオフセット法に要約することができます。

これは、オフセット、次にクロールURLを構築するようにオフセット値を受信するmain()メソッドの変更、であるべきです。コードは以下の通りです

import requests
import re
import json
from requests.exceptions import RequestException

headers = {
    'Content-Type': 'text/plain; charset=UTF-8',
    'Origin': 'https://maoyan.com',
    'Referer': 'https://maoyan.com/board/4',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}

# 爬取网页源代码
def get_one_page(url, headers):
   try:
      response = requests.get(url, headers=headers)
      if response.status_code == 200:
         return response.text
      return None
   except RequestsException:
      return None

def parse_one_page(html):
   pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a'
                        + '.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',
                        re.S)
   items = re.findall(pattern, html)
   for item in items:
      yield {
         'index': item[0],
         'image': item[1],
         'title': item[2],
         'actor': item[3].strip()[3:],
         'time': item[4].strip()[5:],
         'score': item[5] + item[6]
      }

def write_to_file(content):
   with open('result.txt', 'a', encoding='utf-8') as f:
      f.write(json.dumps(content, ensure_ascii=False) + '\n')
      f.close()
      
#传入offest参数
def main(offset):
   url = "https://maoyan.com/board/4?offset="+str(offset)
   html = get_one_page(url, headers)
   for item in parse_one_page(html):
      print(item)
      write_to_file(item)


if __name__ == '__main__':
   for i in range(10):
      main(i*10)

これまでのところ、我々は猫の目をクロール映画TOP100を完了している、すべての映画情報は、テキストファイルに保存されます。
ここに画像を挿入説明

  • 参考:(崔清華付き)のpython3クローラの開発実際の戦闘
リリース3元の記事 ウォンの賞賛2 ビュー137

おすすめ

転載: blog.csdn.net/qq_43645530/article/details/104091597