Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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_List_Beautifulsoup - Fatal编程技术网

为什么我的文本在屏幕上运行,而不是在Python终端上运行?

为什么我的文本在屏幕上运行,而不是在Python终端上运行?,python,list,beautifulsoup,Python,List,Beautifulsoup,为什么要按如下方式打印,我实际上希望看到下面的输出列表: soup = BeautifulSoup(requests.get(url).text, 'html.parser') for tr in soup.findAll("table"): for td in tr.find_all("a"): table = str(td.text) print(table) Code International Languages England en enk

为什么要按如下方式打印,我实际上希望看到下面的输出列表:

soup = BeautifulSoup(requests.get(url).text, 
'html.parser')
for tr in soup.findAll("table"):
    for td in tr.find_all("a"):
        table = str(td.text)
        print(table)

Code
International
Languages
England
en
enk
Welsh
wk
wkk
France
fr
fk

这是因为您正在逐项打印,并且每个
print
都会跟随用换行符打印的数据(除非使用
end
参数明确更改)。要获得您想要的,请使用:

['Code', 'International', 'Languages', 'England', 'en', 'enk', 'Welsh', 'wk', 'wkk', 'France', 'fr', 'fk']
table=[]
对于汤中的tr.findAll(“表”):
对于tr.find_all(“a”)中的td:
表.追加(str(td.text))
打印表
soup = BeautifulSoup(requests.get(url).text, 'html.parser')
print([str(td.text) for tr in soup.findAll("table") for td in tr.find_all("a")])