python - 使用 python pandas 和 beautifulSoup 抓取分页网络表

标签 python string for-loop

我是 python pandas 的初学者,我正在尝试使用漂亮的 soup 包废弃分页表,数据被抓取,但每个单元格的内容都在一行中,我无法获得连贯的 csv 文件

这是我的代码:

import urllib
import urllib.request
from bs4 import BeautifulSoup
import os

file=open(os.path.expanduser("sites_commerciaux.csv"), "wb")

def make_soup(url):
    thepage=urllib.request.urlopen(url)
    soupdata=BeautifulSoup(thepage,"html.parser")
    return soupdata


headers="Nom_commercial_du_Site,Ville,Etat,Surface_GLA,Nombre_de_boutique,Contact"
file.write(bytes(headers,encoding='ascii',errors='ignore'))
save=""
for num in range(0,22): 
    soup=make_soup("http://www.ceetrus.com/fr/implantations-sites-commerciaux?page="+str(num))
    for rec in soup.findAll('tr'):
        saverec=""
        for data in rec.findAll('td'):
            saverec=saverec+","+data.text
            if len(saverec)!=0:
             save=save+"\n"+saverec[1:]

file.write(bytes(save,encoding='ascii',errors='ignore'))

谁能帮我解决这个问题

最佳答案

我做了一些清洁工作。首先,为什么是 bytes 类型?你正在写文字。那为什么是ascii呢?请使用unicode。如果稍后在您的代码中您确实需要将 ascii 编码为 ascii,那么。不推荐使用 findAll,请使用 find_all。您还可能遇到表面值中逗号的问题。最后,尽可能使用上下文管理器(此处:使用文件)

现在对于你的问题,你有两个问题:

  1. 你的测试if len(saverec)!=0:在for循环中,生成了很多 无用的数据。
  2. 您没有删除数据中不需要的空格

.

import urllib
import urllib.request
from bs4 import BeautifulSoup
import os


def make_soup(url):
    thepage=urllib.request.urlopen(url)
    soupdata=BeautifulSoup(thepage,"html.parser")
    return soupdata


save=""
for num in range(0, 22):
    soup=make_soup("http://www.ceetrus.com/fr/implantations-sites-commerciaux?page="+str(num))
    for rec in soup.find_all('tr'):
        saverec=""
        for data in rec.find_all('td'):
            data = data.text.strip()
            if "," in data:
                data = data.replace(",", "")
            saverec=saverec+","+data
        if len(saverec)!=0:
         save=save+"\n"+saverec[1:]
    print('#%d done' % num)

headers="Nom_commercial_du_Site,Ville,Etat,Surface_GLA,Nombre_de_boutique,Contact"
with open(os.path.expanduser("sites_commerciaux.csv"), "w") as csv_file:
    csv_file.write(headers)
    csv_file.write(save)

第一页的输出:

Nom_commercial_du_Site,Ville,Etat,Surface_GLA,Nombre_de_boutique,Contact
ALCORCÓN,ALCORCÓN - MADRID,Ouvert,4298 m²,40,José Carlos GARCIA
Alegro Alfragide,CARNAXIDE,Ouvert,11461 m²,122,
Alegro Castelo Branco,CASTELO BRANCO,Ouvert,6830 m²,55,
Alegro Setúbal,Setúbal,Ouvert,27000 m²,114,
Ancona,Ancona,Ouvert,7644 m²,41,Ettore PAPPONETTI
Angoulême La Couronne,LA COURONNE,Ouvert,6141 m²,45,Juliette GALLOUEDEC
Annecy Grand Epagny,EPAGNY,Ouvert,20808 m²,61,Delphine BENISTY
Anping,Tainan,Ouvert,969 m²,21,Roman LEE
АКВАРЕЛЬ,Volgograd,Ouvert,94025 m²,182,Viktoria ZAITSEVA
Arras,ARRAS,Ouvert,4000 m²,26,Anais NIZON

关于python - 使用 python pandas 和 beautifulSoup 抓取分页网络表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51119584/

相关文章:

python - 如何通过 Google BigQuery 的 Python 客户端库设置现有表过期?

Python 字符串操作问题

c - 我如何在 WinHttpOpenRequest 中使用字符串变量?

java - 不明白为什么这行不通 |循环不会运行

c++ - 在基于 C++ 范围的 for 循环中获取当前元素的索引

algorithm - 创建一个函数,但也要确保我实现了另一个函数。是否可以?

python - 如何在 python 中使用@..以及@property 和@classmethod

python - 如何在 Jupyter notebook 中找到缩进级别?

Python 换出 sys.modules 并不像直觉那样工作

c# - 从文本文件生成随机问题 - C#