Python输出文件为空-Web抓取

时间:2018-08-28 09:45:42

标签: python web-scraping

不知道出了什么问题,但是python代码在尝试执行网络抓取时会输出一个空白文件。代码正在成功执行,但是输出文件中没有任何数据。以下是详细信息。以下代码有什么问题?标题和价格只是我要提取的两个字段。

import requests
import os

from bs4 import BeautifulSoup
URL = 'https://sa.aqar.fm/%D8%A3%D8%B1%D8%A7%D8%B6%D9%8A- 
%D9%84%D9%84%D8%A8%D9%8A%D8%B9/1'
myfile = "Landsv11245.csv"
if os.path.isfile(myfile):

    f = open(myfile, "w", encoding='utf-8')

import requests
import os
 
from bs4 import BeautifulSoup
URL = 'https://sa.aqar.fm/%D8%A3%D8%B1%D8%A7%D8%B6%D9%8A-%D9%84%D9%84%D8%A8%D9%8A%D8%B9/1'
myfile = "Landsv11245.csv"
if os.path.isfile(myfile):
        
        f = open(myfile, "w", encoding='utf-8')
        f.writelines("title" + "," + "price" + "," + "size" + "," + "bed" + "," + "bath" + "\n")
else:    
        f = open(myfile, "w", encoding='utf-8')
        f.writelines("title" + "," + "price" + "," + "size" + "," + "bed" + "," + "bath" + "\n")
    
URL = 'https://sa.aqar.fm/%D8%A3%D8%B1%D8%A7%D8%B6%D9%8A-%D9%84%D9%84%D8%A8%D9%8A%D8%B9/1'
print (URL)
res = requests.get(URL)
soup = BeautifulSoup(res.text, 'lxml')   
f = open(myfile, "a", encoding='utf-8')
for items in soup.select(".content [id^='single-id-']"):
        title = items.select_one("h4 a").text
        price = items.select_one(".prices").text
        f.write(title +","+ price.replace(",", "") + "\n")
f.close()

f.writelines(“标题” +“,” +“价格” +“,” +“大小” +“,” +“床” +“,” +“浴” +“ \ n”)     其他:
        f =打开(myfile,“ w”,编码=“ utf-8”)         f.writelines(“标题” +“,” +“价格” +“,” +“ \ n”)

 URL = 'https://sa.aqar.fm/%D8%A3%D8%B1%D8%A7%D8%B6%D9%8A- 
 %D9%84%D9%84%D8%A8%D9%8A%D8%B9/1'
 print (URL)
 res = requests.get(URL)
 soup = BeautifulSoup(res.text, 'lxml')   
 f = open(myfile, "a", encoding='utf-8')
 for items in soup.select(".content [id^='single-id-']"):
 title = items.select_one("h4 a").text
 price = items.select_one(".prices").text
 f.write(title +","+ price.replace(",", "") + "\n")
 f.close()

0 个答案:

没有答案