Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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 web抓取数据的结果_Python_Web Scraping - Fatal编程技术网

如何用逗号分隔Python web抓取数据的结果

如何用逗号分隔Python web抓取数据的结果,python,web-scraping,Python,Web Scraping,我正试图从不同的网站获取数据和信息。我编写了一个脚本,它工作得很好,但是,当我试图打印结果时,出现了一些问题,例如结果就像句子一样,没有逗号,没有分隔符。没什么,我自己也没试过 这是我正在工作的网站 我试图在结果和结果之间加一个逗号。逗号在结尾,仅此而已 linksname.find_all('p')[i].text + ',' 导入请求 进口bs4 导入csv 输入io response=requests.get('http://www.conditions-de-banque-tunisi

我正试图从不同的网站获取数据和信息。我编写了一个脚本,它工作得很好,但是,当我试图打印结果时,出现了一些问题,例如结果就像句子一样,没有逗号,没有分隔符。没什么,我自己也没试过

这是我正在工作的网站

我试图在结果和结果之间加一个逗号。逗号在结尾,仅此而已

linksname.find_all('p')[i].text + ','
导入请求
进口bs4
导入csv
输入io
response=requests.get('http://www.conditions-de-banque-tunisie.com/banques-en-tunisie.html')
response.status\u代码
soup_obj=bs4.beautifulsou(response.text,“html.parser”)
汤_obj.美化()
#打印('shhh')
linksname=soup\u obj.find(class='bloc-banques-liste')
#linksname.text
textContent=[]
对于范围(0,1)中的i:links=linksname.find_all('p')[i].text
textContent.append(链接)
对于文本内容中的文本:
打印(“-------------------------------”)
打印(文本)
将io.open(“fname.txt”,“w”,encoding=“utf-8”)作为f:f.write(文本)
结果是:

北非国际银行地址:埃纳西姆蒙普莱西大道1002突尼斯:+216 71 950 800传真:+216 71 950 840网址:

卡塔尔国家银行地址:Rue de la citédes sciences-B.p.320-1080突尼斯CedexTé:+216 71 750 000传真:+216 71 235 611网站:

我预计结果如下:

北非国际银行,地址:突尼斯凯雷丁·帕查·恩纳西姆·蒙普莱西尔大街1002号:+216 71 950 800,传真:+216 71 950 840,网址:

或者最好的结果是:

北非国际银行,地址:Kheireddine Pacha Ennassim Montplaisir 1002,+216 71 950 800,+216 71 950 840,:


检查下面的代码,让我知道这是否有帮助

import requests
import bs4
import csv
import io
response = requests.get('http://www.conditions-de-banque-tunisie.com/banques-en-tunisie.html')
response.status_code
soup_obj = bs4.BeautifulSoup(response.text, "html.parser")
soup_obj.prettify()


#print('shhh') 
linksname = soup_obj.find(class_='bloc-banques-liste')
textContent = []
links = linksname.findChildren("div", class_='banques-liste-desc', recursive=True)
links = [" \n ".join([y.text for y in link.findChildren("p")]) for link in links]
print(str(links))

以下是您的代码的简化版本,但在将其与您自己的风格相适应之后,它应该可以让您达到您需要的位置:

from bs4 import BeautifulSoup as bs
import requests

response = requests.get('http://www.conditions-de-banque-tunisie.com/banques-en-tunisie.html')

soup = bs(response.text, "html.parser")

textContent = []
linksname = soup.find(class_='bloc-banques-liste')
for name in linksname:
    entry =   linksname.find_all('p')[0].text
    textContent.append(entry) 
    break

for bank in textContent:
    print(bank.replace(' :',',').strip())
输出:

Al BarakaAdresse, 88, Avenue Hedi Chaker 1002 TunisTé, +216 71 790 000Fax, +21671 780 235Email, contact@albarakabank.com.tnSite web,  http://www.albarakabank.com.tn/ 



Amen BankAdresse, Avenue Mohamed V 1002 Tunis - TunisieTé, (+216) 71 148 000Fax, (+216) 71 833 517Site web,  http://www.amenbank.com.tn/ 
等等。

您可以使用“banques liste desc”来代替使用“bloc banques liste”类来查找对象
这将直接为您提供所有块的列表。
检查以下代码

import requests
import bs4
import csv
import io
response = requests.get('http://www.conditions-de-banque-tunisie.com/banques- 
en-tunisie.html')
response.status_code
soup_obj = bs4.BeautifulSoup(response.text, "html.parser")
soup_obj.prettify()

linksname = soup_obj.find_all(class_='banques-liste-desc')
for i in range(0, len(linksname)):
    name = linksname[i].find('h1').find('a').text
    print(name)
    address = linksname[i].find_all('p')
    for j in range(0, len(address)):
        print(address[j].text)
    print("----------------------------")

在这里,我分别打印了所有的值,而不是使用逗号直接连接它们

谢谢,但有一个小问题,结果很好,但就像一个大段落,而不是一个大团块。你能更新你的代码吗?因为结果我将把它们放在csv中,有了这个结果,我不能接受下面的一个答案,在这一点上。