【问题标题】:Python Won't Run? [closed]Python 不会运行? [关闭]
【发布时间】:2021-05-15 21:15:56
【问题描述】:

enter image description here我正在练习一些数据科学技能,并想尝试这个涉及食品评论和网络抓取的项目(在 Python3 上)。这是它的代码:

import requests
from bs4 import BeautifulSoup

#Used headers/agent because the request was timed out and asking for an agent.
#Using following code we can fake the agent.
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
response = requests.get("https://www.zomato.com/bangalore/top-restaurants",headers=headers)

content = response.content
soup = BeautifulSoup(content,"html.parser")

top_rest = soup.find_all("div",attrs={"class": "bb0 collections-grid col-l-16"})
list_tr = top_rest[0].find_all("div",attrs={"class": "col-s-8 col-l-1by3"})

list_rest =[]
for tr in list_tr:
    dataframe ={}
    dataframe["rest_name"] = (tr.find("div",attrs={"class": "res_title zblack bold nowrap"})).text.replace('\n', ' ')
    dataframe["rest_address"] = (tr.find("div",attrs={"class": "nowrap grey-text fontsize5 ttupper"})).text.replace('\n', ' ')
    dataframe["cuisine_type"] = (tr.find("div",attrs={"class":"nowrap grey-text"})).text.replace('\n', ' ')
    list_rest.append(dataframe)
list_rest

import pandas
df = pandas.DataFrame(list_rest)
df.to_csv("zomato_res.csv",index=False)

问题是当我尝试在我的 OSX 终端上运行它时,它只是给了我一个空白。我认为它应该创建一个 CSV 文件,但我在任何地方都看不到它?

【问题讨论】:

  • 我们可以在运行时获取终端的屏幕截图吗?
  • 已添加!抱歉,我对此有点陌生,但我认为添加屏幕截图有效?

标签: python python-3.x pandas beautifulsoup


【解决方案1】:

问题是,你不能在这里使用 CSS 类名——它们经常变化。要获取有关餐馆的数据,请使用以下示例:

import requests
import pandas as pd
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0"
}
response = requests.get(
    "https://www.zomato.com/bangalore/top-restaurants",
    headers=headers,
)

soup = BeautifulSoup(response.content, "html.parser")

sections = soup.find_all(
    lambda tag: tag.name == "section"
    and [t.name for t in tag.find_all(recursive=False)] == ["a", "div"]
)

list_rest = []
for section in sections:
    name = section.img["alt"]
    addr = section.find("a", color=True)
    typ = [a.text for a in addr.find_next("div").find_all("a")]
    list_rest.append(
        {
            "rest_name": name,
            "rest_address": addr.text,
            "cuisine_type": ", ".join(typ),
        }
    )

df = pd.DataFrame(list_rest)
print(df)
df.to_csv("data.csv", index=False)

打印:

                      rest_name           rest_address                                       cuisine_type
0                    Chaat King   Rajarajeshwari Nagar      Quick Bites, Street Food, Sandwich, Fast Food
1                        Onesta        Electronic City  Casual Dining, Café, Pizza, Fast Food, Dessert...
2              Nammura Upachara     Basaveshwara Nagar  Quick Bites, South Indian, North Indian, Stree...
3                Bakasura Bandi             Nagarbhavi       Quick Bites, Fast Food, Chinese, Street Food
4       New Punjabi Food Corner        Electronic City               Casual Dining, North Indian, Chinese
5                        Noon's                    HSR                               Quick Bites, Biryani
6             New Malabar Hotel           New BEL Road  Quick Bites, Chinese, South Indian, Rolls, Nor...
7                         Sagar          Seshadripuram  Casual Dining, North Indian, Street Food, Fast...
8               Amma's Pastries             Nagarbhavi                           Bakery, Bakery, Desserts
9          Shawarma Grill House            Rajajinagar             Quick Bites, Chinese, Biryani, Mughlai
10             One For The Road             Whitefield                       Bar, North Indian, Fast Food
11                  Gawky Goose       Old Airport Road  Bar, Casual Dining, Italian, South Indian, Nor...
12  Byg Brewski Brewing Company                 Hennur  Microbrewery, Continental, North Indian, Itali...
13             The Bier Library  Koramangala 6th Block  Microbrewery, Continental, North Indian, Pizza...
14                    Just Cake            Magadi Road  Bakery, Bakery, Desserts, Street Food, Fast Fo...
15            New Shanthi Sagar            Rajajinagar  Quick Bites, Chinese, Street Food, North India...
16               Chichaba's Taj            Frazer Town     Casual Dining, North Indian, Chinese, Desserts
17               Bucket Biryani           Brigade Road                      Casual Dining, Biryani, Kebab
18              Razakhiya Hotel             HBR Layout  Quick Bites, Rolls, Chinese, North Indian, Sea...
19                 Sai Cafe Veg           Malleshwaram                             Quick Bites, Fast Food
20                     Parkview               JP Nagar  Quick Bites, South Indian, North Indian, Stree...
21                  Grill House   Rajarajeshwari Nagar         Quick Bites, North Indian, Chinese, Andhra
22          Goli Vada Pav No. 1             Nagarbhavi                Quick Bites, Street Food, Fast Food
23           Savoury Restaurant            Frazer Town  Quick Bites, Arabian, Mughlai, North Indian, C...
24     Chaayos - Meri Wali Chai           Malleshwaram  Café, Cafe, Tea, Street Food, Fast Food, Bever...
25                        SMOOR      Bannerghatta Road        Dessert Parlor, Desserts, Cafe, Pizza, Thai
26              SLAY Coffee Bar  Koramangala 5th Block                 Café, Coffee, Fast Food, Beverages
27           Indiranagar Social            Indiranagar  Bar, Café, North Indian, Chinese, Burger, Momo...
28   Third Wave Coffee Roasters               JP Nagar            Café, Cafe, Coffee, Fast Food, Desserts
29                       Aubree                MG Road     Bakery, Cafe, Bakery, Fast Food, Italian, Thai

并保存data.csv(来自 LibreOffice 的屏幕截图):

【讨论】:

    猜你喜欢
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2019-01-21
    • 2020-11-22
    相关资源
    最近更新 更多