Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法刮取文本_Python_Web Services_Scrape - Fatal编程技术网

Python 无法刮取文本

Python 无法刮取文本,python,web-services,scrape,Python,Web Services,Scrape,我想知道这些网站的名称。 所以,我用这个片段来做这件事 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

我想知道这些网站的名称。 所以,我用这个片段来做这件事

    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')
这非常有效,将Google作为标题,并将输出刷新为test_data.txt

但是,当我尝试运行与web服务相同的代码时,它不起作用。我得到一个空的文本文件。 我点击此URL在本地服务器上运行此web服务

另一件让我更加焦虑的事情是,当我为msn.com运行web服务时,它对web服务和web服务都很有效


任何帮助都会很感激

这是烧瓶吗?如果是这样,则需要返回要发送给用户的字符串。打印语句写入web服务器日志。您应该将get_title函数的最后一行替换为:

return list1.encode('utf-8')
return list1.encode('utf-8')