Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 美联能';找不到文本_Python_Python 3.x_Beautifulsoup_Urllib - Fatal编程技术网

Python 美联能';找不到文本

Python 美联能';找不到文本,python,python-3.x,beautifulsoup,urllib,Python,Python 3.x,Beautifulsoup,Urllib,我正在尝试使用urllib和beautiful soup用python编写一个scraper。我有一个用于新闻故事的csv URL,对于大约80%的页面,scraper可以工作,但是当故事顶部有图片时,脚本不再拉动时间或正文。我很困惑,因为soup.find和soup.find_似乎都不会产生不同的结果。我尝试了各种不同的标签,它们应该能够捕获文本以及“lxml”和“html.parser” 代码如下: testcount = 0 titles1 = [] bodies1 = [] times1

我正在尝试使用urllib和beautiful soup用python编写一个scraper。我有一个用于新闻故事的csv URL,对于大约80%的页面,scraper可以工作,但是当故事顶部有图片时,脚本不再拉动时间或正文。我很困惑,因为soup.find和soup.find_似乎都不会产生不同的结果。我尝试了各种不同的标签,它们应该能够捕获文本以及“lxml”和“html.parser”

代码如下:

testcount = 0
titles1 = []
bodies1 = []
times1 = []

data = pd.read_csv('URLsALLjun27.csv', header=None)
for url in data[0]:
try:
    html = urllib.request.urlopen(url).read()
    soup = BeautifulSoup(html, "lxml")

    titlemess = soup.find(id="title").get_text() #getting the title
    titlestring = str(titlemess) #make it a string
    title = titlestring.replace("\n", "").replace("\r","")
    titles1.append(title)

    bodymess = soup.find(class_="article").get_text() #get the body with markup
    bodystring = str(bodymess) #make body a string
    body = bodystring.replace("\n", "").replace("\u3000","") #scrub markup
    bodies1.append(body) #add to list for export

    timemess = soup.find('span',{"class":"time"}).get_text()
    timestring = str(timemess)
    time = timestring.replace("\n", "").replace("\r","").replace("年", "-").replace("月","-").replace("日", "")
    times1.append(time)

    testcount = testcount +1 #counter
    print(testcount)
except Exception as e:
    print(testcount, e)
下面是我得到的一些结果(那些标记为“nonetype”的是那些标题被成功提取但正文/时间为空的结果)

一,

2“非类型”对象没有属性“获取文本”

任何帮助都将不胜感激!谢谢


编辑:我没有“10个信誉点”,因此我无法发布更多链接进行测试,但如果您需要更多页面示例,我会对它们进行评论。

问题是,网站上没有包含图片的
class=“article”
,与
“类”相同:“time”
。因此,您似乎必须检测网站上是否有图片,然后如果有图片,请按以下方式搜索日期和文本:

try:
    bodymess = soup.find(class_="article").get_text()

except AttributeError:
    bodymess = soup.find('img').findNext().get_text()

try:
    timemess = soup.find('span',{"class":"time"}).get_text()

except AttributeError:
    timemess = soup.find(id="pubtime").get_text()
有关日期,请尝试:

timemess = soup.find(id="pubtime").get_text()
就正文而言,这篇文章似乎只是图片的标题。因此,您可以尝试以下方法:

bodymess = soup.find('img').findNext().get_text()
简而言之,
soup.find('img')
查找图像,然后
findNext()
转到下一个块,该块恰好包含文本

因此,在您的代码中,我将执行以下操作:

try:
    bodymess = soup.find(class_="article").get_text()

except AttributeError:
    bodymess = soup.find('img').findNext().get_text()

try:
    timemess = soup.find('span',{"class":"time"}).get_text()

except AttributeError:
    timemess = soup.find(id="pubtime").get_text()
作为web抓取的一般流程,我通常使用浏览器访问网站本身,并首先在浏览器中查找网站后端的元素