如何取消没有任何源代码的数据?

时间:2019-01-05 11:04:50

标签: python-3.x web-scraping beautifulsoup

scrap.py

# code to scrap the links from the html

from bs4 import BeautifulSoup
import urllib.request

data = open('scrapFile','r')
html = data.read()
data.close()
soup = BeautifulSoup(html,features="html.parser")
# code to extract links

links = []
for div in soup.find_all('div', {'class':'main-bar z-depth-1'}):

    # print(div.a.get('href'))
    links.append('https://godamwale.com' + str(div.a.get('href')))


print(links)
file = open("links.txt", "w")
for link in links:

    file.write(link + '\n')
    print(link)

使用此代码,我已经成功获得了链接列表。但是,当我想从其html页面中的那些链接中抓取数据时,这些链接没有任何包含数据的源代码,并且要提取它们很困难。我已经使用了selenium驱动程序,但是对我来说效果不佳。 我想从下面的链接中抓取数据,该链接包含html部分中的数据,这些数据具有客户详细信息,许可和自动化,商业详细信息,现场,运营详细信息。我想提取这些数据的名称,位置,联系电话和类型。

https://godamwale.com/list/result/591359c0d6b269eecc1d8933

它是这里的链接。如果有人找到解决方案,请给我。

2 个答案:

答案 0 :(得分:2)

使用浏览器中的Developer工具,您会发现,每次访问该链接时,都会有一个针对https://godamwale.com/public/warehouse/591359c0d6b269eecc1d8933的请求,该请求返回一个可能包含您要查找的数据的json响应。

Python 2.x:

import urllib2, json
contents = json.loads(urllib2.urlopen("https://godamwale.com/public/warehouse/591359c0d6b269eecc1d8933").read())
print contents

Python 3.x:

import urllib.request, json
contents = json.loads(urllib.request.urlopen("https://godamwale.com/public/warehouse/591359c0d6b269eecc1d8933").read().decode('UTF-8'))
print(contents)

答案 1 :(得分:0)

在这里,网站的主要问题似乎是加载需要花费时间,这就是它返回不完整页面源的原因。您必须等待页面完全加载。在下面的代码中注意time.sleep(8)这一行:

from bs4 import BeautifulSoup
import requests
from selenium import webdriver
import time

CHROMEDRIVER_PATH ="C:\Users\XYZ\Downloads/Chromedriver.exe" 

wd = webdriver.Chrome(CHROMEDRIVER_PATH)

responce = wd.get("https://godamwale.com/list/result/591359c0d6b269eecc1d8933")

time.sleep(8)  # wait untill page loads completely 

soup = BeautifulSoup(wd.page_source, 'lxml')

props_list = []
propvalues_list = []

div = soup.find_all('div', {'class':'row'})
for childtags in div[6].findChildren('div',{'class':'col s12 m4 info-col'}):
    props = childtags.find("span").contents
    props_list.append(props)

    propvalue = childtags.find("p",recursive=True).contents
    propvalues_list.append(propvalue)

print(props_list)
print(propvalues_list)

注意:代码将在2个单独的列表中返回构造详细信息。