【问题标题】:Unable to prints the Names and links in python无法在 python 中打印名称和链接
【发布时间】:2021-12-08 20:11:00
【问题描述】:

我在提取名称和链接时遇到困难,它没有任何响应,但会打印价格。

我抓取的链接是:https://sehat.com.pk/categories/Over-The-Counter-Drugs/Diarrhea-and-Vomiting-/

import requests
from bs4 import BeautifulSoup
import pandas as pd
import time

url = 'https://sehat.com.pk/categories/Over-The-Counter-Drugs/Diarrhea-and-Vomiting-/'
r = requests.get(url)
time.sleep(6)
soup = BeautifulSoup(r.content, 'html.parser')
content = soup.find_all('div', class_ = 'col-md-12 pr-0 pl-0')

for property in content:


    links = property.find('div',{'class': 'col-md-12 d-table-cell align-middle'})['href']
    name= property.find('img', class_ = 'img-fluid').text.strip()
    price= property.find('div', class_ = 'ProductPriceRating d-table-cell text-center pl-1 pr-1 align-middle').text.strip()
    print(name,links,price)

【问题讨论】:

    标签: python pandas beautifulsoup request webdriver


    【解决方案1】:

    你可以这样试试

    import requests
    from bs4 import BeautifulSoup
    import pandas as pd
    import time
    import csv 
    
    url = 'https://sehat.com.pk/categories/Over-The-Counter-Drugs/Diarrhea-and-Vomiting-/'
    r = requests.get(url)
    # time.sleep(6)
    soup = BeautifulSoup(r.content, 'html.parser')
    content = soup.find_all('div', class_ = 'col-md-12 pr-0 pl-0')
    # print(content)
    
    
    header = ['url', 'item', 'price']
    data = []
    
    for property in content:
        link =[i['href'] for i in property.findAll("a")][0]
    
    
        title = [i.getText(strip=True) for i in property.find_all("a")][1]
    
        
    
        price = [i.getText(strip=True) for i in property.find_all('div',{'class':"ProductPriceRating"})][0]
        data.append([link, title, price])
    
    print(data)
    df = pd.DataFrame(data, columns=header)
    
    df.to_csv("products.csv")
    

    【讨论】:

    • 谢谢,从哪里找到标题请找到我
    • 它只是重命名列而已。我刚刚更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2021-05-26
    • 2021-06-06
    • 2022-11-14
    • 2016-01-07
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多