簡體   English   中英

使用 BeautifulSoup 抓取問題

[英]Scraping Issue using BeautifulSoup

我是使用 python 抓取數據的新手,它給了我錯誤和 idk 如何解決它。

這是我的代碼。

from bs4 import BeautifulSoup
import requests

html_text = requests.get('https://www.olx.com.pk/mobile-phones_c1453/q-iphone-x')
# print(html_text)
soup = BeautifulSoup(html_text, 'lxml')

info = soup.findAll('ul', class_ = 'de20bb96 _021e1d41')

for one_info in info:
    single_phone = soup.find('li', class_ = 'Listing').text

    print(single_phone)

這是錯誤信息!!

E:\\Softwares\\Anaconda\\python.exe "D:/Courses/Webscraping using python/olxweb.py" Traceback(最近一次調用最后一次):文件“D:/Courses/Webscraping using python/olxweb.py”,第 7 行在湯= BeautifulSoup(HTML_TEXT, 'LXML')文件“E:\\軟件\\阿納康達\\ lib中\\站點包\\ bs4_初始化_.py”,線310,在初始化elif的LEN(標記)<= 256和(類型錯誤:“響應”類型的對象沒有 len() 進程已完成,退出代碼為 1

html_text變量上使用.content

import requests
from bs4 import BeautifulSoup

html_text = requests.get(
    "https://www.olx.com.pk/mobile-phones_c1453/q-iphone-x"
)
soup = BeautifulSoup(html_text.content, "lxml")  # <-- use .content

for one_info in soup.select('li[aria-label="Listing"]'): # <-- select by aria-label= atribute
    print(one_info.get_text(strip=True, separator=" "))

印刷:

Featured Rs 64,000 Iphone X for sale Chauburji Park, Lahore 5 days ago
Featured Rs 88,000 Iphone x 256gb (PTA Approved) 10/10 Anarkali, Lahore 2 weeks ago
Featured Rs 48,000 iphone X DHA Defence, Islamabad 3 days ago
Featured Rs 76,000 iphone x 256gb pta approved Shalamar Town, Lahore 1 week ago
Featured Rs 68,000 IPHONE X 64GB PTA APPROVED Dharampura, Lahore 2 weeks ago
Featured Rs 69,500 Iphone X Kot Khawaja Saeed, Lahore 1 week ago
Rs 85,000 Iphone x Multan 3 minutes ago
Rs 72,000 iphone X 64Gb Pta approve Gujrat 9 minutes ago
Rs 110,000 iphone x  s max for sale condition 10/9 pta aproved Wazirabad 14 minutes ago
Rs 3,000 iphone x orignal panel touch not working Askari, Lahore 15 minutes ago
Rs 40,000 IPhone x 64 gb Bahadurabad, Karachi 20 minutes ago
Rs 82,000 Iphone X 256 GB Ghazi Road, Lahore 22 minutes ago
Rs 93,000 iPhone X 64 GB Peshawar 26 minutes ago
Rs 30,000 iphone x bypass Model Town Extension, Lahore 29 minutes ago
Rs 75,000 iphone x few month used battery heath 94% Mandi Bahauddin 29 minutes ago
Rs 67,000 iphone x for sale Attock 30 minutes ago
Rs 65,000 iphone x Haidery, Karachi 33 minutes ago
Rs 65,000 iphone x 64gb white colour 7th Avenue, Islamabad 38 minutes ago
Rs 95,000 iphone x white Gulzar Colony, Lahore 53 minutes ago
Rs 80,000 iphone x white colour 64gb Multan 55 minutes ago

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM