Python爬取國家資訊

2022-04-04 17:43:01 字數 2496 閱讀 3037

# -*- coding: utf-8 -*-

''' _author_: gao yang

_time_:2017.8.25

'''import urllib.request

import csv

import json

import lxml.html

import lxml.cssselect

import time

def download(url, num_retries=2):

headers =

request = urllib.request.request(url,headers=headers)

try:

# 將網頁進行編碼

# 通過網頁逆向工程得到ajax資料

html = download('')

ajax = json.loads(html)

for record in ajax['records']:

countries.add(record['country'])

countries = sorted(countries)

# 將得到的所以國家的名字儲存到country.txt檔案中

with open('country.txt','r+',encoding='utf-8') as f:

f.write(str(sorted(countries)))

def get_couninfo(url):

number = 1

# 遍歷集合countries中的國家information

for country in countries:

time.sleep(10)

# **的網頁格式為

full_url = url + country + "-" + str(number)

# 得到完整的站點網域名稱後,通過lxml解析特定的標籤,並將得到的字典新增到陣列attr中

attr.append(crawlercountry(full_url))

# 在陣列attr中遍歷每乙個國家的field中的key,得到列表row

row = [attr[number-1][field] for field in fields]

# 寫入csv檔案

writer.writerow(row)

number += 1

if __name__ == '__main__':

countries = set()

attr =

url = ""

fields = ('country', 'area', 'population', 'iso', 'capital', 'continent', 'tld', 'currency_code', 'currency_name', 'phone', 'languages')

writer = csv.writer(open('country.csv', 'r+',encoding='utf-8'))

writer.writerow(fields)

get_counname(countries)

get_couninfo(url)

國家資訊 德語

德語中一些國家資訊的表示方法 國家 國家人 男 國家人 女 國家的語言 indien 印度 der inder die inderin indisch englisch england 英格蘭 der engl nder die engl nderin englisch englisch itali...

Python 爬取網頁資訊

對於本次學習爬蟲中的一些總結 1.要熟練掌握基礎知識,包括一些基礎的語法 2.正規表示式的正確使用,建議學習北理工的python爬蟲課程 3.先寫大框架再新增小的功能解析 4.對程式異常處理要熟練,盡量使用try.excep結構 5.對於列表字串資料的基本使用到位,比如增刪改查等 6.思路必須清晰 ...

Python 爬取天氣資訊

第一次python部落格,僅作紀念。import requests import re from bs4 import beautifulsoup requests庫從網上獲取資源,re bs4 庫,用來提取需要的資訊。開啟要爬取的 右擊檢視其源 找到感興趣的內容,如下 2020年01月12日 星期...