我使用此代码提取信息
# _*_ coding:utf-8 _*_
import urllib2
import urllib
import re
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def grabHref(url,localfile):
html = urllib2.urlopen(url).read()
html = unicode(html,'gb2312','ignore').encode('utf-8','ignore')
soup = BeautifulSoup(html)
myfile = open(localfile,'wb')
for link in soup.select("div > a[href^=http://www.karmaloop.com/kazbah/browse]"):
for item in BeautifulSoup(urllib2.urlopen(link['href']).read()).select("div > a[href^=mailto]"):
contactInfo = item.get_text()
print link['href']
print contactInfo
myfile.write(link['href'])
myfile.write('\r\n')
myfile.write(contactInfo)
myfile.write('\r\n')
myfile.close()
def main():
url = "http://www.karmaloop.com/brands"
localfile = 'Contact.txt'
grabHref(url,localfile)
if __name__=="__main__":
main()
但我仍然只能得到电子邮件地址在这里,我怎么能得到电话号码和地址?谢谢
看看这个:http://stackoverflow.com/questions/11709079/parsing-html-python – RafaelC
@RafaelCardoso我读到了。但是如何在“|”之后获得信息?我的意思是,获取[email protected]很容易,但很难得到电话和地址 –
也许['split']的文档(https://docs.python.org/3/library/stdtypes.html#str .split)将向您展示如何提取这些“硬”部分。另外,如果你展示你自己尝试过的某种形式的代码,那么在将来考虑你会得到(更好的)答案。如果你特意写出获取电子邮件地址很容易,那么为什么你没有复制你在问题中使用的代码?看看[写完美的问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)和[如何问](https://stackoverflow.com/help /如何对问)。 –