LXML "HTTP 리소스를로드하지 못했습니다"오류가있는 웹 페이지를 검색 할 수 없습니다.
문제
안녕하세요 그래서 브라우저에서 아래 링크를 열고 코드가 작동하지 않고 작동하지 않습니다.이 링크는 실제로 뉴스 사이트의 조합과 다른 파일 url.txt에서 호출 된 기사의 확장자입니다.나는 일반적인 웹 사이트 (www.google.com)로 코드를 시도했으며 완벽하게 작동합니다.
import sys
import MySQLdb
from mechanize import Browser
from bs4 import BeautifulSoup, SoupStrainer
from nltk import word_tokenize
from nltk.tokenize import *
import urllib2
import nltk, re, pprint
import mechanize #html form filling
import lxml.html
with open("url.txt","r") as f:
first_line = f.readline()
#print first_line
url = "http://channelnewsasia.com/&s" + (first_line)
t = lxml.html.parse(url)
print t.find(".//title").text
.
그리고 이것이 내가 얻는 오류입니다.
이는 url.txt
의 내용입니다./ news/asiapacific/australia-to-send-armed/1284790.html
해결책
URL의 &s
부분으로 인해 분명히 필요하지 않습니다.
url = "http://channelnewsasia.com" + first_line
.
또한 URL 부품은 urljoin()
를 사용하여 더 낫습니다.:
from urlparse import urljoin
import lxml.html
BASE_URL = "http://channelnewsasia.com"
with open("url.txt") as f:
first_line = f.readline()
url = urljoin(BASE_URL, first_line)
t = lxml.html.parse(url)
print t.find(".//title").text
.
Australia to send armed personnel to MH17 site - Channel NewsAsia
. 제휴하지 않습니다 StackOverflow