Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.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_Python 2.7_Web Scraping_Lxml - Fatal编程技术网

如何在python中显示不带[''']的内容

如何在python中显示不带[''']的内容,python,python-2.7,web-scraping,lxml,Python,Python 2.7,Web Scraping,Lxml,我正在用python中的lxml抓取一个网页 from lxml import html import requests page=requests.get(url) tree=html.fromstring(page.text) name=tree.xpath(xpath) print name 它正在显示:[“马修”] 我想展示:马修 有解决方案吗?它正在打印['Matthew'],因为它是一个包含单个元素的列表,字符串'Matthew' 您可以通过简单的索引将其从列表中删除 name =

我正在用python中的lxml抓取一个网页

from lxml import html
import requests
page=requests.get(url)
tree=html.fromstring(page.text)
name=tree.xpath(xpath)
print name
它正在显示:[“马修”]

我想展示:马修

有解决方案吗?

它正在打印['Matthew'],因为它是一个包含单个元素的列表,字符串'Matthew'

您可以通过简单的索引将其从列表中删除

name = tree.xpath(xpath)[0]
这将删除第一个元素


请注意,如果列表为空,这将返回一个索引器。

如果列表中只有一个元素,如您所述['Matthew'],则使用列表索引0。如果有更多元素,请相应地更改索引。但是如果出现错误,比如没有项目出现,它将引发错误


例如,在您的情况下,尝试使用打印名称[0]

您知道这些字符具有语法含义吗?方括号表示列表,引号表示字符串。