遭遇例

Python

カテゴリー: エラー  閲覧数:360 配信日:2017-05-26 12:57


修正前


▼index.py
import urllib.parse
import urllib.request
from bottle import route

@route('/')
def index():
 url = 'https://gihyo.jp/dp'
 req = urllib.request.Request(url)
 with urllib.request.urlopen(req) as response:
    the_page = response.read()
 return(the_page)



修正後


Firefoxに偽装してアクセス
▼index.py
import urllib.parse
import urllib.request
from bottle import route, view

@route('/')
def index():
 url = 'https://gihyo.jp/dp'
 user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
 headers = {'User-Agent': user_agent}
 req = urllib.request.Request(url,headers=headers)
 with urllib.request.urlopen(req) as response:
    the_page = response.read()

 return(the_page)



urllib パッケージを使ってインターネット上のリソースを取得するには

週間人気ページランキング / 6-21 → 6-27
順位 ページタイトル抜粋 アクセス数
1 Python用語 16
2 インポート | インポートシステム 1
2 AttributeError: type object 'datetime.datetime' has no attribute 'datetime' | AttributeError(エラー) 1
2026/6/28 5:05 更新