1
나는 웹 사이트의 제목을 얻으려고하고있었습니다. 그래서, 나는이 완벽하게 작동이텍스트를 긁을 수 없습니다
sys.stdout = open("test_data.txt", "w")
url2 = "https://www.google.com/"
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A'}
req = urllib2.Request(url2, None, headers)
req.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
html = urllib2.urlopen(req, timeout=60).read()
soup = BeautifulSoup(html)
# Extract title
list1 = soup.title.string
print list1.encode('utf-8')
을이 조각을 사용 제목으로 구글를 제공하고 test_data.txt 할 수있는 출력을 플러시합니다.
그러나 웹 서비스와 동일한 코드를 실행하려고하면 작동하지 않습니다. 나는 빈 텍스트 파일을 얻는다. 나는 나를 더 걱정했다 내 로컬 http://0.0.0.0:8881/get_title
from bottle import route, run, request
@route('/get_title')
def get_title():
sys.stdout = open("test_data.txt", "w")
url2 = "https://www.google.com/"
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A'}
req = urllib2.Request(url2, None, headers)
req.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
html = urllib2.urlopen(req, timeout=60).read()
soup = BeautifulSoup(html)
# Extract title
list1 = soup.title.string
print list1.encode('utf-8')
if __name__ == "__main__":
run(host='0.0.0.0', port=8881, debug=True)
또 다른 일에이 웹 서비스를 실행하려면이 URL을 내가 msn.com에 대한 웹 서비스를 실행하면 (두 조각을 위해 잘 작동합니다 타격하고 심지어 웹 서비스).
도움이 감사 할 것입니다 !!
으로 바꿔야합니다. 그런 다음 msn.com과 같은 다른 URL에서 작동하는 이유는 무엇입니까? – x0v