出発点となる中国のネットワーク人気ランキングトップ100を取得するPythonクローラー(クイックスタート、初心者は必須です!)



Python Crawler Get Starting Point Chinese Network Popularity Ranking Top 100 Quick Start



このブロガーがあなたにもたらすのは、Pythonクローラーを使用して、中国のWebのトップ100人気ランキングを共有するための出発点を取得することです。学習の過程でクローラーの魅力を感じていただければ幸いです!
URLから始めましょうhttps://www.qidian.com/all/出発点の中国のウェブサイトのホームページに来てください!
画像
画像
URLの構成とリソースを取得する必要のあるページ数に応じて、最初にURLのリストを作成できます。
http://a.qidian.com/?page={}'.format(str(i)) for i in range(1,6)

具体的なコードは次のとおりです。



''' @File: Get the top 100 popularity ranking of the starting point Chinese network.py @Time : 2019/10/21 22:31 @Author: Encapsulation bacteria @Software: PyCharm Please indicate the original author Creation is not easy, just for sharing ''' # Import related libraries import xlwt import requests from lxml import etree import time # Initialize the list and store it in the crawler data all_info_list = [] def get_info(url): html = requests.get(url) selector = etree.HTML(html.text) # Locate the big label, loop in turn, get the detailed link url of each novel on each page infos = selector.xpath('//ul[@class='all-img-list cf']/li') # Traverse the links to get detailed information about each novel for info in infos: # Title title = info.xpath('div[2]/h4/a/text()')[0] # Author author = info.xpath('div[2]/p[1]/a[1]/text()')[0] # Style 1 style1 = info.xpath('div[2]/p[1]/a[2]/text()')[0] # Style 2 style2 = info.xpath('div[2]/p[1]/a[3]/text()')[0] # Style style = style1 + style2 # Degree of completion complete = info.xpath('div[2]/p[1]/span/text()')[0] # Novel introduction introduce = info.xpath('div[2]/p[2]/text()')[0].strip() info_list = [title, author, style, complete, introduce] # Save the data in the list all_info_list.append(info_list) # Set sleep time time.sleep(1) # Program main entrance if __name__ == '__main__': urls = ['http://a.qidian.com/?page={}'.format(str(i)) for i in range(1,6)] for url in urls: get_info(url) time.sleep(5) # Define header header = ['title', 'author', 'style', 'complete', 'introduce'] # Create workbook book = xlwt.Workbook(encoding='utf_8') # Create worksheet sheet = book.add_sheet('Shee1') # python range() The function can create a list of integers, which is generally used in a for loop. # Python len() method returns the length of the object (character, list, tuple, etc.) or the number of items. for h in range(len(header)): # Write header sheet.write(0, h, header[h]) i = 1 # Store the data in the xls table by looping through for list in all_info_list: j = 0 for data in list: sheet.write(i, j, data) # View Results print(data) j += 1 i += 1 # After the data is stored, save the workbook to the local path book.save('qidianxiaoshuo.xls')

有効性の検証:
画像
最終結果をローカルxlsファイルに保存したので、直接開いて表示します。
画像
qidianxiaoshuo.xlsファイル
画像

上記の効果が見られたら、おめでとうございます。これで完了です。とてもおもしろいと思いますか~~これで共有は終わりです。恩恵を受けた友達は、好きで注意を払うことを忘れないでください。菌はもっとシンプルで楽しいテクノロジーを立ち上げます٩(๑>◡<๑)۶



画像