2016-10-13 3 views
0

이 특정 사이트의 데이터를 크롤링하려고하지만 어려운 부분은 스크립트 내부에 필요한 세부 정보입니다. 출력은 여기웹 사이트의 스크립트에 특정 텍스트를 표시하는 방법

Product Details Burgundy woven blouson top with gathers, has a round neck, sleeveless, criss-cross detail on the back Material & Care Crepe Hand-wash cold해야합니다 내가 코드를한다 나는 BeautifulSoup로 셀레늄을 사용하여 시도하지만 또한 내가

http://www.myntra.com/tops/rare/rare-burgundy-crepe-blouson-top/1437335/buy

을 필요하지 않았다 견딜 수 없는.

from bs4 import BeautifulSoup 
import urllib 
x=urllib.urlopen("http://www.myntra.com/tops/rare/rare-burgundy-crepe-blouson-top/1437335/buy") 
soup2 = BeautifulSoup(x, 'html.parser') 


for i in soup2.find_all('p',atttrs={'class':'pdp-product-description-content'}): 
    print i.text 

답변

0

정규식 &json을 사용하는 것이 좋습니다. 먼저 정규식을 사용하여 페이지 소스에서 스크립트 변수를 추출한 다음 추출 된 텍스트를 json 개체로로드해야합니다. 마지막으로 최종 json dict에서 productDescriptors 키 아래에 데이터가 저장됩니다.

import re 
import json 
import urllib 
from bs4 import BeautifulSoup 

x = urllib.urlopen("http://www.myntra.com/tops/rare/rare-burgundy-crepe-blouson-top/1437335/buy") 

soup2 = BeautifulSoup(x, 'html.parser') 

product_data = re.findall('({"pdpData".+?)</script>', re.sub('\n', ' ', soup2.prettify())) 

product_description = json.loads(product_data[0])['pdpData']['productDescriptors'] 

print product_description 
0

한 가지 방법은 다음과 같습니다

from bs4 import BeautifulSoup 
from selenium import webdriver 

url = 'http://www.myntra.com/tops/rare/rare-burgundy-crepe-blouson-top/1437335/buy' 

driver = webdriver.Chrome() 
driver.maximize_window() 
driver.get(url) 
req = driver.page_source 
soup = BeautifulSoup(req,'html.parser') 

producttitle = soup.find('h6',{'class':'pdp-product-description-title'}) 
productbody = soup.find('p',{'class':'pdp-product-description-content'}) 

print producttitle.text, productbody.text 


matc = soup.findAll('h6',{'class':'pdp-product-description-title'})[1:] 
for x in matc: 
    print x.text 

matd = soup.findAll('p',{'class':'pdp-product-description-content'})[1:] 
for y in matd: 
    print y.text 

이 인쇄됩니다

Product Details Burgundy woven blouson top with gathers, has a round neck, sleeveless, criss-cross detail on the back 
Material & Care 
CrepeHand-wash cold 
관련 문제